2016-08-02 76 views
2

我使用Promised-Mongo从NodeJS后端代码连接MongoDB和Promises。它运行良好,直到我启用了MongoDB的客户端访问控制。当我运行这段代码,我得到 “无法验证” 消息“:“无法验证”与promised-mongo

var pmongo = require('promised-mongo').compatible(); 
var db = pmongo('myusername:[email protected]/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的代码(即没有承诺......)正常工作:?

var mongodb = require('mongodb'); 
var uri = 'mongodb://myusername:[email protected]/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}'); 
    }); 
}); 

任何想法

+0

现在连接,并担心后来收集 var db = pmongo('mydb');从文档..希望这可以帮助 –

+0

已经尝试过,没有工作... – krishnamurti

+0

Am..from你的消息,其中e.message说“无法验证”...这是问题。检查mongodb的凭据。你有所有的访问权限。并尝试使用mongo shell连接相同的凭证。 –

回答

0

在github存储库中的几个问题(见herehere)看起来像使用这个库的身份验证是完全破坏的。每第二个链接,大多数人似乎正在用promisify,bluebird,要么 一个薄的自定义包装。