简体   繁体   中英

MongoError - Authentication error (password with @)

Here is my code in my NodeJS application, to connect to my MongoDB engine :

 const collection = 'mynewcollection';
 const password = 'passwordwithan@';
 const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}@mymongobase.net/${collection}`;

 // Connect using the connection string
 MongoClient.connect(mongoUrl, {useNewUrlParser: true}, function(err, db) {
   console.log(err.toString())
 });

I get an authentication error. I tried several things to handle the '@' character and reading the documentation I thought that it was the good one... But it is still failing even if the user and password are the good one.

Is the API correctly used ? Do you understand what is wrong ?

Thanks in advance.

OK I found the solution. If you use :

const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}@mymongobase.net/${collection}`;

Mongo will try to connect with admin/password defined on the collection .

If you don't put the collection in the mongo url :

const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}@mymongobase.net`;

Then you can use the super user defined on all the mongo engine.

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