【发布时间】:2016-08-02 18:13:28
【问题描述】:
我正在使用Promised-Mongo 将 MongoDB 与来自 NodeJS 后端代码的 Promises 连接起来。它工作得很好,直到我启用了 MongoDB 的客户端访问控制。当我运行此代码时,我收到“无法验证”消息”:
var pmongo = require('promised-mongo').compatible();
var db = pmongo('myusername:mypassword@localhost/mydb', ['candidates']);
db.candidates.save(req.body)
.then(function () {
// never reached here
})
.catch(function (e) {
// it reached here, where e.message says "could not authenticate"
});
纯 MongoDB 代码(即没有 Promises...)工作正常:
var mongodb = require('mongodb');
var uri = 'mongodb://myusername:mypassword@localhost/mydb';
mongodb.MongoClient.connect(uri, function (err, db) {
if (err) {
// never reached here
}
var candidates = db.collection('candidates');
candidates.insert(req.body, function(err, result) {
if (err) {
// never reached here
}
res.send('{result: success}');
});
});
有什么想法吗?
【问题讨论】:
-
现在连接,以后再考虑收集 var db = pmongo('mydb');来自文档..希望这会有所帮助
-
已经试过了,没用...
-
Am..from you message where e.message 说“无法验证”...这就是问题所在。检查 mongodb 的凭据。您是否拥有所有访问权限。并尝试使用 mongo shell 连接相同的凭据。
-
喜欢这个 mongo -u
-p --authenticationDatabase yourAuthDB -
我使用第二个 sn-p 代码(有效)来“证明”这不是错误凭据的问题。 mongo shell 也适用于我。这与 save() 方法有关(或者我之前错过的东西),但我就是不知道到底是什么......
标签: node.js mongodb authentication