"无法验证"与承诺 - 蒙戈

时间:2016-08-02 18:13:28

标签: node.js mongodb authentication

我正在使用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}');
    });
});

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

github存储库中的几个问题(请参阅herehere)看起来像使用此库进行身份验证完全被破坏了。根据第二个链接,大多数人似乎通过promisify,bluebird或薄自定义包装器等承诺包装官方库。