【问题标题】:MongoDB - running parallel promises with nodejs driver is not working with multiple databasesMongoDB - 使用 nodejs 驱动程序运行并行承诺不适用于多个数据库
【发布时间】:2017-04-20 19:28:30
【问题描述】:

我有这段代码,我只从 1 个 Promise 获得结果,我通过评论其中一个来单独尝试每个,它们实际上没有错误地解决,但是当它们在一起时,我只得到第一个的结果。 但不是来自Promise.all,只有Promise 中的console.log 被调用

var mongodb = require('mongodb');
var Promise = require('bluebird');

var $ = mongodb.MongoClient.connect('mongodb://nobody:nobody@localhost/dbauth');
$.then(
(db) => {
    var pp = [];
    pp.push(new Promise(
     (resolve, reject) => {
        var dbx = db.db('db1');
        dbx.authenticate('user1', 'user1').then((x) => {
            console.log(x); // true
            var o = dbx.stats();
            o.then(
               (result) => {
                  console.log(result);
                  resolve(result);
               }
            ).catch(
               (err) => {
                   reject(err);
               }
            );
        });
     }));

    pp.push(new Promise(
     (resolve, reject) => {
       var dbx = db.db('db2');
       dbx.authenticate('user2', 'user2').then((x) => {
            console.log(x);
            var o = dbx.stats();
            o.then(
               (result) => {
                  console.log(result);
                  resolve(result);
               }
            ).catch(
               (err) => {
                   reject(err);
               }
            );
       });
    }));

    return Promise.all(pp).then(
        (res) => {
            res.forEach(console.log);
        }
    ).catch(console.log);
}).catch(console.log);

这是什么打印

true // from console.log(x); of promise db1 -> auth successful
true // from console.log(x); of promise db2 -> auth successful
{ db: 'db1',
  collections: 2,
  views: 0,
  objects: 0,
  avgObjSize: 0,
  dataSize: 0,
  storageSize: 8192,
  numExtents: 0,
  indexes: 2,
  indexSize: 8192,
  ok: 1 } // from console.log(res) of promise db2

但如果我从 both 承诺的代码中删除此部分并改为调用 resolve(x)

,相同的代码将正常工作
                o.then(
                   (result) => {
                      console.log(result);
                      resolve(result);
                   }
                ).catch(
                   (err) => {
                       reject(err);
                   }
                );

为什么实际上在 Promise.all 之前执行了承诺?


更新 -> 这不起作用

我把我的代码改成这样了,但它也不起作用

var mongodb = require('mongodb');
var Promise = require('bluebird');

var $ = mongodb.MongoClient.connect('mongodb://nobody:nobody@localhost/dbauth');
$.then((db) => {

    var db1 = db.db('db1');
    var db2 = db1.db('db2');

    var p1 = db1.authenticate('user1', 'user2').then((x) => {
        return db1.stats();
    });

    var p2 = db2.authenticate('user2', 'user2').then((x) => {
        return db2.stats();
    });

    return Promise.all([p1,p2]).then(console.log);
}).catch(console.log);

更新 2 -> 这可行,但不是我想要的

我把我的代码改成这样,它成功了! ...但这不是我想要的,我想要的是让两个 Promise 都以异步方式执行

var $ = mongodb.MongoClient.connect('mongodb://nobody:nobody@localhost/dbauth');
$.then((db) => {
    var db1 = db.db('db1');
    var p1 = db1.authenticate('user1', 'user2').then((x) => {
        return db1.stats().then(
            (res) => {
                console.log(res);
                var db2 = db1.db('db2');
                return db2.authenticate('user2', 'user2').then((x) => { 
                    return db2.stats();
                });
            }
        );
    });
    p1.then(console.log);
}).catch(console.log);

似乎是 MongoDB 的问题?


更新 3 -> 这不起作用

const px = (dbx, user, pass) => {
    return dbx.authenticate(user, pass).then((x) => {
        return dbx.stats()
    });
}

var $ = mongodb.MongoClient.connect('mongodb://nobody:nobody@localhost/dbauth');
$.then((db) => {
    Promise.all([
        px(db.db('db1'), 'user1', 'user1'),
        px(db.db('db2'), 'user2', 'user2')
    ]).then(console.log);
}).catch(console.log);

【问题讨论】:

  • 避免promise构造函数反模式...dbx.authenticate返回一个promise,不需要将它包装在promise中
  • @JaromandaX 我更改了我的代码,但也没有用
  • UPDATE 2 - 它们仍然是异步的,但串联而不是并行
  • 顺便说一句-您的代码只是jsfiddle.net/y8yajo12的冗长版本:p
  • @JaromandaX 这是直接在终端上实验的结果,我按照你说的做了,但还是不行

标签: javascript node.js mongodb promise


【解决方案1】:

我根本没有阅读你的问题。但是,这个:

return Promise.all([pp]).then(
    (res) => {
        res.forEach(console.log);
    }
).catch(console.log);

似乎是错误的,因为我相信 pp 是一个数组。意思是:[[whatever is in pp]]

【讨论】:

  • sorry no 那是编辑问题的错误,实际上我也尝试Promise.all(pp)
【解决方案2】:

这是一个 MongoDB 问题,我们无法同时运行 auth 操作

https://jira.mongodb.org/browse/NODE-984

【讨论】:

    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 2018-06-09
    • 2020-06-16
    • 2015-07-26
    • 1970-01-01
    • 2021-06-09
    • 2014-07-09
    • 1970-01-01
    相关资源
    最近更新 更多