connecting mongoose to mongoDB atlas and nodejs

This is my mongoose connection code:

mongoose.connect("mongodb+srv://Sarthak:*******:[email protected]/test?retryWrites=true",{ useNewUrlParser: true })
        console.log("Connected to mongo database");
        console.log("Error connecting mongo database",err);

I got the errors below, any idea how to fix this?

Error connecting mongo database { MongoParseError: Unescaped colon in authority section at parseConnectionString (/home/sarthak/Projects/thePracticalGuide/node_modules/mongodb-core/lib/uri_parser.js:250:23) at QueryReqWrap.dns.resolveTxt [as callback] (/home/sarthak/Projects/thePracticalGuide/node_modules/mongodb-core/lib/uri_parser.js:126:7) at QueryReqWrap.onresolve [as oncomplete] (dns.js:240:10) name: ‘MongoParseError’, [Symbol(mongoErrorContextSymbol)]: {} }

This is the error I am getting while connection


Try to use this way of connection too

 /* I've removed the ":Wb" between your password and @clus... As mongoDB atlas website didn't use that in my generated connection string */
  mongoose.connect("mongodb+srv://Sarthak:*******",{ useNewUrlParser: true });    

  mongoose.connection.on('error', (err) => {
    console.error(`Mongoose connection error: ${err}`);

Otherwise things to consider:

  • Make sure that you’ve added your connecting device IP address in the IP whitelist in the security tap of your cluster in mongoDB atlas website (if you’ve already done that you might try giving permission to every IPs by adding to check there’s no issue regarding to this)
  • Regarding to the password, you may got confused with the mongoDB atlas login password, than the user you made for the cluster (this is a common mistake that mongoDB atlas newbies make).
  • If not and you’re using the right password, you can try to delete the user and re-create it again in the security tab (in the clusters view in mongoDB atlas website). First consider giving the user a very basic password without any special character and try connecting again to ensure that it’s not an encoding issue.
  • At the end if none of above worked with you try making connection via shell. (you can see the connection string in the mongoDB atlas website, in the connect section of your cluster. There you can get better logs regarding to the cause of your problem.