【问题标题】:"could not authenticate" with promised-mongo用promise-mongo“无法验证”
【发布时间】: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


【解决方案1】:

根据 github 存储库中的几个问题(请参阅 herehere),使用此库进行身份验证似乎完全被破坏了。根据第二个链接,大多数人似乎都在通过诸如 promisify、bluebird 或瘦自定义包装器之类的东西来包装官方库。

【讨论】:

    猜你喜欢
    • 2013-08-15
    • 2023-01-31
    • 1970-01-01
    • 2022-01-10
    • 2014-12-10
    • 2019-08-20
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    相关资源
    最近更新 更多