【问题标题】:error returning data foreach find mongodb array错误返回数据foreach查找mongodb数组
【发布时间】:2020-09-12 17:56:00
【问题描述】:

我正在查阅一份文件,结果我查阅了另一份文件model,但最后,什么都没有返回给我,foreach 不等待await

ShoppingCart.find({
   "userId": id
}).then(async ShoppingCart => {
   let distinctTypes = ShoppingCart[0].productsCart;
   distinctTypes.sort(function(r, u) {
      return r.manufacturerId > u.manufacturerId ? 1 : r.manufacturerId < u.manufacturerId ? -1 : 0
   });
   let products = [];
   let data2 = await distinctTypes.forEach(async function(thisType) {
      let id = thisType.productId;
      let data = await Product.findById(id).then(Product => {
         thisType.productId = Product;
         products.push(thisType);
         return products;
      });
      return data; ///at this point the information is correct
   });
   return data2;
});

“数据”:空

【问题讨论】:

    标签: javascript node.js mongodb mongoose async-await


    【解决方案1】:

    发生这种情况是因为 forEach 没有以同步方式执行。 请改用 For of 循环。

    详细回答:- Using async/await with a forEach loop

    【讨论】:

    • 非常好的 await 答案(distinctTypes 的常量产品){}
    【解决方案2】:

    Array.prototype.forEach 不等待是正确的。相反,您应该使用Array.prototype.mapPromise.all

    ShoppingCart.find({
       "userId": id
    }).then(async cart => {
       let distinctTypes = cart[0].productsCart;
       distinctTypes.sort(function(r, u) {
          return r.manufacturerId > u.manufacturerId ? 1 : r.manufacturerId < u.manufacturerId ? -1 : 0
       });
       const distinctTypesWithProducts = await Promise.all(
         distinctTypes.map(async function(thisType) {
            let id = thisType.productId;
            const product = await Product.findById(id)
            thisType.product = product // changed assignment of product to thisType.product
            return thisType
         });
       );
       return distinctTypesWithProducts;
    });
    

    您可以使用我开始的project 简化上述内容。欢迎投稿。

    const { pipe, assign, map, get } = require('rubico')
    
    // userId => cart
    const findCartByUserID = userId => ShoppingCart.find({ userId })
    
    // [distinctType] => [distinctType] sorted
    const sortDistinctTypes = distinctTypes => distinctTypes.sort((r, u) => {
      return r.manufacturerId > u.manufacturerId ? 1 : r.manufacturerId < u.manufacturerId ? -1 : 0
    })
    
    // distinctType => distinctTypeWithProduct
    const assignProduct = assign({
      product: pipe([
        get('productId'), // distinctType => productId
        Product.findById, // productId => product
      ]), // distinctType.product = product; return distinctType
    })
    
    // userId => [distinctTypeWithProduct, distinctTypeWithProduct, ...]
    const getDistinctTypesWithProductFromUserID = pipe([
      findCartByUserID,
      // userId => cart
    
      get([0, 'productsCart']),
      // cart => distinctTypes
    
      sortDistinctTypes,
      // distinctTypes => sortedDistinctTypes
    
      map(assignProduct),
      // sortedDistinctTypes => [distinctTypeWithProduct, distinctTypeWithProduct, ...]
    ])
    
    getDistinctTypesWithProductFromUserID(id)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      相关资源
      最近更新 更多