Fail in connection to MongoDB Node.JS Driver in Atlas

Hi everyone,

I am currently trying to connect an app to the MongoDB Driver connection through Node.js, but I have had some errors in the code.

First, the error was pointing that the use of “URL=mongodb+srv://” was not valid and I should instead use just “URL=mongodb://”. When I corrected as suggested by the console I started to get this error

Mongoose disconnected from database
Mongoose connection error: MongoError: failed to connect to server [finstearchcluster1.gmhq7qx.mongodb.net:27017] on first connect [Error: getaddrinfo ENOTFOUND finstearchcluster1.gmhq7qx.mongodb.net
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26) {
name: ‘MongoError’
}]

Not quite sure what is wrong on my side. If someone can give me a hint or lead me to a related topic would be awesome. Thank you!

Hi @Cristian_Barria,

finstearchcluster1.gmhq7qx.mongodb.net is an SRV record, so you need to ensure your connection string in Mongoose is something like mongodb+srv://<user>:<pass>@finstearchcluster1.gmhq7qx.mongodb.net/.

The error you likely got is because you tried to append a port (27017) to the connection string when connecting using SRV.

1 Like

Hi @alexbevi ,

I am just using the string provided by the driver connector

mongodb+srv://CristianBarria:@finstearchcluster1.gmhq7qx.mongodb.net/?retryWrites=true&w=majority

I am changing the password when I place it in VS Code

Should the URL go like this then?
mongodb+srv://CristianBarria:@finstearchcluster1.gmhq7qx.mongodb.net/

mongodb+srv://CristianBarria:<password>@finstearchcluster1.gmhq7qx.mongodb.net/ would be correct. Does this work when you connect via the mongosh shell? Have you configured the appropriate IP Access List to ensure you can connect to the cluster?

Hi @alexbevi ,

with the new URL I am receiving the following error:

D:\..\server\node_modules\muri\lib\index.js:28
    throw new Error('Invalid mongodb uri "' + str + '". Must begin with "mongodb://"'); 
          ^

Error: Invalid mongodb uri mongodb+srv://CristianBarria:BVyQIGhLHyVgRlfg@finstearchcluster1.gmhq7qx.mongodb.net/.". Must begin with "mongodb://"
    at muri (D:\MERN Dashboard\server\node_modules\muri\lib\index.js:28:11)
    at Connection.openUri (D:\MERN Dashboard\server\node_modules\mongoose\lib\connection.js:766:18)
    at Mongoose.connect (D:\MERN Dashboard\server\node_modules\mongoose\lib\index.js:262:17)
    at file:///D:/MERN%20Dashboard/server/index.js:24:10
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
Emitted 'error' event on NativeConnection instance at:
    at Connection.error (D:\MERN Dashboard\server\node_modules\mongoose\lib\connection.js:673:8)
    at Connection.openUri (D:\MERN Dashboard\server\node_modules\mongoose\lib\connection.js:775:10)
    at Mongoose.connect (D:\MERN Dashboard\server\node_modules\mongoose\lib\index.js:262:17)
    at file:///D:/MERN%20Dashboard/server/index.js:24:10
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)

Node.js v19.9.0

I have set the IP access list prior to the connection and even set up to public sharing for this try out. I haven´t look mongosh shell yet, but I´ll try it now

Hi @alexbevi ,

I solved the issue. There was an issue with the versioning on the packages I installed in my code. Now its running. Thank you!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.