0
votes

I am new to node.js and mongoDB Atlas. I am having an issue connecting to Mongo Atlas using the Node.js version 3.0 or later connection string.

const MongoDB = 'mongodb+srv://<user>:<password>@cluster0-nnezr.mongodb.net/<dbname>?retryWrites=true&w=majority'

When I use this connection string I get the following error -

MongoDB connection error: Error: querySrv ETIMEOUT _mongodb._tcp.cluster0-nnezr.mongodb.net at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19) { errno: undefined, code: 'ETIMEOUT', syscall: 'querySrv', hostname: '_mongodb._tcp.cluster0-nnezr.mongodb.net' } (node:38809) UnhandledPromiseRejectionWarning: Error: querySrv ETIMEOUT _mongodb._tcp.cluster0-nnezr.mongodb.net at QueryReqWrap.onresolve [as oncomplete] (dns.js:203:19) (Use node --trace-warnings ... to show where the warning was created) (node:38809) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside ofan async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:38809) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections thatare not handled will terminate the Node.js process with a non-zero exit code.

I have found a workaround using the previous connection string for node.js version 2.2.12 -

mongodb://<user>:<password>@cluster0-shard-00-00-nnezr.mongodb.net:27017,cluster0-shard-00-01-nnezr.mongodb.net:27017,cluster0-shard-00-02-nnezr.mongodb.net:27017/<dbname>?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority

When connecting I have to add {useUnifiedTopology: true}

mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });

Can anyone give me a little insight on what im doing wrong with the new connection string?

Thanks!

1
are you using an .env file? try writing { useNewUrlParser: true, useFindAndModify: false, useUnifiedTopology: true }Eli Sigal
No not using a .env file. Tried adding {useFindAndModify: false} but still getting the same errorKevin Beall

1 Answers

0
votes

have write user, password and maybe also database ,

   const MongoDB = 'mongodb+srv://<user>:<password>@cluster0-nnezr.mongodb.net/<dbname>?retryWrites=true&w=majority'

user = your mongodb user name

password = your mongodb password

dbname = Database name

and don't forget to create a new user in MongoDB atlas.