【问题标题】:Cannot get document `find` promises from Mongoose model无法从 Mongoose 模型中获取文档“find”承诺
【发布时间】:2021-10-23 06:55:09
【问题描述】:

我有一个User 模型和一个Product 模型。 User 将 objectIds 数组存储到 Products 作为引用。在User 成员函数中,我试图查找用户订购的所有产品,我在下面使用此函数,但是,执行不会超出我评论的位置。

// Get each productObjectId, and get Promises that will resolve to productDocs
var getProductDocs Promises = [] // Store promises here
// 'productsTracking' is an array in `User`
this.productsTracking.forEach((productObjectId) => {
   getProductDocsPromises.push(Product.findById(productObjectId).exec()) // Execution halts here, and raises a blank error
})        

我做错了什么?

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query mongoose-schema


    【解决方案1】:

    使用这个

    // Get each productObjectId, and get Promises that will resolve to productDocs
    var getProductDocs Promises = [] // Store promises here
    // 'productsTracking' is an array in `User`
    this.productsTracking.forEach((productObjectId) => {
       getProductDocsPromises.push(Product.findById(productObjectId)) // Execution halts here, and raises a blank error
    })     
    Promise.all(getProductDocsPromises)
    .then()
    .catch()
    

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 2018-11-19
      • 2019-08-15
      • 2018-08-17
      • 1970-01-01
      • 2021-12-01
      • 2018-07-04
      • 2023-01-28
      • 2021-05-23
      相关资源
      最近更新 更多