Node.js 在array.foreach Sequelize中查询数据库

Node.js 在array.foreach Sequelize中查询数据库,node.js,express,sequelize.js,Node.js,Express,Sequelize.js,使用array.forEach修改sequelize中的resultset 我想循环返回的shoppingCart,访问每个元素的数据库,添加一些属性并返回 const promiseInvoices = []; await ShoppingCart.findAll({ where: { // eslint-disable-next-line object-shorthand cart_id: cartItem.cart_id, }, }).th

使用array.forEach修改sequelize中的resultset

我想循环返回的shoppingCart,访问每个元素的数据库,添加一些属性并返回

const promiseInvoices = [];

  await ShoppingCart.findAll({
    where: {
      // eslint-disable-next-line object-shorthand
      cart_id: cartItem.cart_id,
    },
  }).then(result => {
   result.forEach(cartItem2 => {
      const prod = Product.findByPk(cartItem2.product_id);
      console.log(prod);

      let cartItem3 = {};
      const total = prod.price * cartItem2.quantity;
      cartItem3.cart_id =  cartItem2.cart_id;
      cartItem3.product_id =cartItem2.product_id;
      cartItem3.attributes  =cartItem2.attributes;
      cartItem3.quantity  =cartItem2.quantity ;
      cartItem3.price = prod.price;
      cartItem3.name = prod.name;
      cartItem3.image = prod.image2;
      cartItem3.item_id = cartItem2.item_id;
      cartItem3.subtotal = total;

    return promiseInvoices.push(cartItem3);
    });
  });
返回shoppingCart,访问每个shoppingCart对象的数据库,并获取产品以填写每个shoppingCart项目的值。对Product.findByPk的调用将返回如下可用承诺:

Promise [Object] {
      _bitField: 0,
      _fulfillmentHandler0: undefined,
      _rejectionHandler0: undefined,
      _promise0: undefined,
      _receiver0: undefined }
  • 所有Sequelize CRUD调用都是异步的,这意味着它们返回一个承诺,因此您必须等待它们。返回的是
    Promise
    对象而不是包含详细信息的产品,这是因为您没有等待
    findByPk
    调用完成

  • forEach
    构造不支持异步操作。即使将
    array.forEach
    回调设置为异步,它也不会工作,并且仍然返回
    Promise
    对象,而不是包含详细信息的产品

  • 要解决此问题,您需要等待async
    findByPk
    调用,并使用支持异步操作的循环。您可以将循环用于您的用例

    const promiseInvoices = []
    
    await ShoppingCart.findAll({
        where: {
            cart_id: cartItem.cart_id
        }
    }).then(result => {
    
        // for...of loop that supports await
        for await (const cartItem2 of result){
            // Make sure to wait on all your sequelize CRUD calls
            const prod = await Product.findByPk(cartItem2.product_id)
    
            // It will now wait for above Promise to be fulfilled and show the proper details
            console.log(prod)
    
            let cartItem3 = {}
            const total = prod.price * cartItem2.quantity
            cartItem3.cart_id = cartItem2.cart_id
            cartItem3.product_id = cartItem2.product_id
            cartItem3.attributes = cartItem2.attributes
            cartItem3.quantity = cartItem2.quantity
            cartItem3.price = prod.price
            cartItem3.name = prod.name
            cartItem3.image = prod.image2
            cartItem3.item_id = cartItem2.item_id
            cartItem3.subtotal = total
    
            // Simple push will work in this loop, you don't need to return anything
            promiseInvoices.push(cartItem3)
        }
    })
    

    完美的我在结果之前添加了async,效果很好。谢谢