Reputation: 144
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)]: {} }
Upvotes: 3
Views: 9621
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
[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.
Upvotes: 0
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
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
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:
Upvotes: 1
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
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