Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js MongoError-身份验证错误(密码为@)_Node.js_Mongodb - Fatal编程技术网

Node.js MongoError-身份验证错误(密码为@)

Node.js MongoError-身份验证错误(密码为@),node.js,mongodb,Node.js,Mongodb,以下是我在NodeJS应用程序中的代码,用于连接到我的MongoDB引擎: const collection = 'mynewcollection'; const password = 'passwordwithan@'; const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}@mymongobase.net/${collection}`; // Connect using the connection str

以下是我在NodeJS应用程序中的代码,用于连接到我的MongoDB引擎:

 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())
 });
我得到一个身份验证错误。我尝试了几种方法来处理“@”字符,并阅读了文档,我认为这是一个很好的方法。。。 但即使用户和密码是正确的,它仍然失败

API是否正确使用?你知道怎么回事吗

提前谢谢。

好的,我找到了解决方案。 如果您使用:

const mongoUrl = `mongodb://admin:${encodeURIComponent(password)}@mymongobase.net/${collection}`;
Mongo将尝试使用集合中定义的管理员/密码连接

如果未将集合放入mongo url中:

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

然后您可以使用在所有mongo引擎上定义的超级用户。

可能的重复项经过进一步调查,@不是问题。事实上,当我尝试连接Mongo时,它找不到我的超级用户“admin”。。。如果我创建了一个新用户并尝试与之连接,就可以了。我不明白为什么超级用户(在我的引擎的每个数据库中似乎都很常见)在我尝试登录时无法识别。