Issue
Hi I am trying to connect mongoDB atlas database to my flask api.
when I use following code (with the correct password):
from flask import Flask, jsonify, request, redirect
from flask_pymongo import PyMongo
app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb+srv://test:<password>@cluster0.xdweu.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
mongo = PyMongo(app)
#db_operations = mongo.db.<COLLECTION_NAME>
db_operations = mongo.db.test
@app.route('/test')
def test():
return "App is working perfectly"
@app.route("/create")
def create():
new_user = {'Name' : 'xyz', 'Age' : 20}
db_operations.insert_one(new_user)
#print(user['Name'],'Created successfully')
result = {'result' : 'Created successfully'}
return result
if __name__ == '__main__':
app.run(debug=True)
I get a running api while when I test "/test" but when I use the "/create/ I get an
"pymongo.errors.ServerSelectionTimeoutError"
on the browser.
here is the whole traceback:
Traceback (most recent call last):
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\flask\app.py", line 2091, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\flask\app.py", line 2076, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\flask\app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "D:\Dev\api_test\appi\app.py", line 21, in create
db_operations.insert_one(new_user)
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\collection.py", line 705, in insert_one
self._insert(document,
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\collection.py", line 620, in _insert
return self._insert_one(
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\collection.py", line 609, in _insert_one
self.__database.client._retryable_write(
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\mongo_client.py", line 1551, in _retryable_write
with self._tmp_session(session) as s:
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\contextlib.py", line 135, in __enter__
return next(self.gen)
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\mongo_client.py", line 1948, in _tmp_session
s = self._ensure_session(session)
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\mongo_client.py", line 1935, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\mongo_client.py", line 1883, in __start_session
server_session = self._get_server_session()
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\mongo_client.py", line 1921, in _get_server_session
return self._topology.get_server_session()
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\topology.py", line 520, in get_server_session
session_timeout = self._check_session_support()
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\topology.py", line 504, in _check_session_support
self._select_servers_loop(
File "C:\Users\umut8\AppData\Local\Programs\Python\Python310\Lib\site-packages\pymongo\topology.py", line 218, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 617be28dbcf14ff29dcb83b2, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('cluster0-shard-00-00.xdweu.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('cluster0-shard-00-01.xdweu.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>, <ServerDescription ('cluster0-shard-00-02.xdweu.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('connection closed')>]>
Thanks for any upcoming help
Solution
I heard, that you can fix this error by downgrading your pymongo version. Try to downgrade by: pip install pymongo==2.8 --upgrade
Answered By - SchelmEntwicklerJustin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.