Sarthak Srivastav
Sarthak Srivastav

Reputation: 144

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 })
    .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)]: {} }

This is the error I am getting while connection

Upvotes: 3

Views: 9621

Answers (6)

Sucheta Shrivastava
Sucheta Shrivastava

Reputation: 293

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

enter image description here

 [2]   It will take you here -->

enter image description here

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

enter image description here

I followed the above steps and was able to login.

Upvotes: 0

Luca Fagioli
Luca Fagioli

Reputation: 13369

You need to encode your password in the connection string:

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

Upvotes: 2

user11551802
user11551802

Reputation: 31

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.

Upvotes: 3

Arash Abedin
Arash Abedin

Reputation: 66

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.

Upvotes: 1

Joyce Lee
Joyce Lee

Reputation: 386

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

var uri = encodeURI('mongodb+srv://Sarthak:*******:[email protected]/test?retryWrites=true');

Upvotes: 1

chuck_sum
chuck_sum

Reputation: 111

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.

Upvotes: 1

Related Questions