【问题标题】:Mongoose/MongoDB - How to use promise with aggregate queriesMongoose/MongoDB - 如何在聚合查询中使用 Promise
【发布时间】:2017-10-19 05:44:44
【问题描述】:

我正在尝试通过 mongoose 对 MongoDB 中的 2 个不同集合执行 2 个查询,然后将它们的结果组合为 REST API 响应。

例子:

var result1 = Model1.aggregate([<operations here>]).exec()

var result2 = Model2.aggregate([<operations here>]).exec()

var allDone = Promise.all(result1,result2)

allDone.then(function(data1,data2){
//Do something with both data
})

我收到此错误 TypeError:无法读取未定义的属性“readPreference”

当回调的函数签名不是 function(err,docs){...

时会发生这种情况

如果我为 Aggregators 使用回调,它可以工作,但我不想链接回调/查询,并认为这种方式会更有效。

我找到了这个Mongoose aggregate cursor promise

但想知道这是否可以通过更简单的方式使用本机承诺。 我也不想像上面的 SO 答案中解释的那样遍历光标。

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:
    var allDone = Promise.all(result1,result2) 
    

    应该是

    var allDone = Promise.all([result1,result2])
    

    【讨论】:

      猜你喜欢
      • 2020-08-02
      • 2015-10-16
      • 2020-05-16
      • 2020-10-18
      • 2018-09-09
      • 2021-08-23
      • 2015-07-22
      • 1970-01-01
      • 2021-09-24
      相关资源
      最近更新 更多