Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't MongoDB connect to my NodeJS app?

In nodejs server, mongodb isn't connecting. The error code on terminal showing

[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Server started on port 5000
MongoParseError: URI does not have hostname, domain name and tld
    at parseSrvConnectionString (/home/psayeed1990/programming/node/node_passport_login-master/node_modules/mongodb/lib/core/uri_parser.js:41:21)
    at parseConnectionString (/home/psayeed1990/programming/node/node_passport_login-master/node_modules/mongodb/lib/core/uri_parser.js:516:12)
    at connect (/home/psayeed1990/programming/node/node_passport_login-master/node_modules/mongodb/lib/operations/connect.js:266:3)
    at ConnectOperation.execute (/home/psayeed1990/programming/node/node_passport_login-master/node_modules/mongodb/lib/operations/connect.js:191:5)
    at executeOperation (/home/psayeed1990/programming/node/node_passport_login-master/node_modules/mongodb/lib/operations/execute_operation.js:83:26)
    at MongoClient.connect (/home/psayeed1990/programming/node/node_passport_login-master/node_modules/mongodb/lib/mongo_client.js:216:10)
    at /home/psayeed1990/programming/node/node_passport_login-master/node_modules/mongoose/lib/connection.js:632:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/psayeed1990/programming/node/node_passport_login-master/node_modules/mongoose/lib/connection.js:629:19)
    at Mongoose.connect (/home/psayeed1990/programming/node/node_passport_login-master/node_modules/mongoose/lib/index.js:328:15)
    at Object.<anonymous> (/home/psayeed1990/programming/node/node_passport_login-master/app.js:18:4)
    at Module._compile (internal/modules/cjs/loader.js:1128:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:983:32)
    at Function.Module._load (internal/modules/cjs/loader.js:891:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
  name: 'MongoParseError',
  [Symbol(mongoErrorContextSymbol)]: {}

This is the code to connect

dbPassword = 'mongodb+srv://sayeed:'+ encodeURIComponent('123456') + 'mongodb://localhost:27017/node-passport-login';

module.exports = {
    mongoURI: dbPassword
};

The full nodejs app was taken from Traversy Media's github page Node Passport Login.

like image 730
Abu Sayeed Mondal Avatar asked Dec 08 '25 08:12

Abu Sayeed Mondal


2 Answers

I have also faced this issue, I updated my password using autogenerate password, it worked for me.

EXAMPLE

If your password in plain-text is p@ssw0rd'9'!, you would need to encode your password as:

p%40ssw0rd%279%27%21

https://docs.atlas.mongodb.com/troubleshoot-connection/#special-characters-in-connection-string-password

like image 141
Justin J Avatar answered Dec 10 '25 23:12

Justin J


The mongodb connection url you have provided is incorrect, which in your case is the dbPassword. If you want to connect to a locally hosted mongodb database then do the following.

Start mongod in a terminal and the change the dbPassword to the local url like this

dbPassword="mongodb://localhost:27017/dbName"

or you can use the url directly inside app.js like this

mongoose.connect("mongodb://localhost:27017/dbName")

If this is not what you want and you want to do what Brad did which is to connect to a cloud database you need to first make an account on mongodb cloud, login and then create a cluster after which you need to copy the URL.

It's easier to use the local mongodb server in my opinion. You only need cloud databases once you wish to deploy your application.

like image 21
Vivek p.a Avatar answered Dec 11 '25 00:12

Vivek p.a