简体   繁体   中英

connecting mongoose to mongoDB atlas and nodejs

This is my mongoose connection code:

mongoose.connect("mongodb+srv://Sarthak:*******:Wb@cluster0-jli2a.mongodb.net/test?retryWrites=true",{ useNewUrlParser: true })
    .then(()=>{
        console.log("Connected to mongo database");
    })
    .catch((err)=>{
        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)]: {} }

这是我在连接时遇到的错误

Just go to atlas website security tab and edit the password of the user and make sure you do not use "@" or ":". That's it.

您需要在连接字符串中对密码进行编码:

const connectionString = `mongodb://yourUsername:${encodeURIComponent('yourPassword')}@127.0.0.1:27017/mydb`;

The error description is pretty clear - do you have a colon in your password? The typical connectionstring format is "mongodb+srv:[username:password@]host1..." so an unescaped colon would throw a parse error.

I had this same problem, also with "unescaped colons" even though they were very clearly escaped. Try this:

var uri = encodeURI('mongodb+srv://Sarthak:*******:Wb@cluster0-jli2a.mongodb.net/test?retryWrites=true');

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:*******@cluster0-jli2a.mongodb.net/test?retryWrites=true",{ useNewUrlParser: true });    

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

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 0.0.0.0/0 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.

I was getting "Unescaped colon in authority section" using MongoDB Compass when entering the connection string.

For which -

I went into "Create Free Cluster" button and made a cluster.

Then I got the connection string. However, while entering the available connection string I got the error.

I just changed the password by logging into mongodb atlas.

Here is the procedure I used -->

 [1]  To change the password - click on encode URI

在此处输入图片说明

 [2]   It will take you here -->

在此处输入图片说明

  [3]   Click on the edit button from the screen above and change your password.

在此处输入图片说明

I followed the above steps and was able to login.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM