我正在使用Promised-Mongo将MongoDB与来自NodeJS后端代码的Promise连接起来。它工作正常,直到我启用了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}');
});
});
有什么想法吗?