Why can't MongoDB connect to my NodeJS app?

Viewed 4823

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.

3 Answers

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.

In my case, I had '*' and '#' at the end of the password string. I also didn't want to use auto generated password.

Before:

password*#

After:

password%2A%23

You can use online tool to url encode your password

https://www.url-encode-decode.com/

Related