我使用Promised Mongo从NodeJS后端代码连接MongoDB和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}');
});
});
知道吗?
根据github存储库中的几个问题(请参阅此处和此处),使用此库进行身份验证似乎完全失败了。根据第二个链接,大多数人似乎通过promisify、bluebird或薄的自定义包装器等方式用promise包装官方图书馆。