Mysql 使用sequelize多次更新同一行的正确方法

Mysql 使用sequelize多次更新同一行的正确方法,mysql,node.js,sequelize.js,Mysql,Node.js,Sequelize.js,我有一个订单表,我想再次购买,订单表包含一列productDetails。在本专栏中,我有一个包含产品的数组 要执行此再次购买操作,我需要在购物车中放置相同的数组。及时更新重要信息检查是否有库存,产品是否在目录中处于活动状态等,对于阵列中的每个产品,我将在我的购物车表上进行更新,基本上,我将尝试以下操作: 我正在使用node.js和sequelize.js来实现这一点: //得到旧订单 const order=wait order.findOne{ 包括:[ { 型号:OrderProduct,

我有一个订单表,我想再次购买,订单表包含一列productDetails。在本专栏中,我有一个包含产品的数组

要执行此再次购买操作,我需要在购物车中放置相同的数组。及时更新重要信息检查是否有库存,产品是否在目录中处于活动状态等,对于阵列中的每个产品,我将在我的购物车表上进行更新,基本上,我将尝试以下操作:

我正在使用node.js和sequelize.js来实现这一点:

//得到旧订单 const order=wait order.findOne{ 包括:[ { 型号:OrderProduct, }, ], 其中:{ 医嘱ID, }, }; //如果购物车内有东西,请清理cartDetails列 等待CartService.cleanorder.userId; //addProduct执行检查并将产品插入购物车 等待承诺 order.OrderProducts.mapasync数据=>{ wait CartService.addProductorder.userId,data.productMarketId,data.quantity; } ; 也许addProduct只插入了一个产品。如果我们有更多,映射将正确执行,但只在列表中添加一个产品

当我运行debug时,首先,当我在addProduct上的订单上有两个产品时,代码调用两次,因此,获取购物车数据的select始终为空,并且只有最后一个产品被正确插入

2种产品的预期行为:

1. clean cart
2. enter on iterator (map or something else) (product 0):

  2.1. Select cart details (which is empty on first time)
  2.2. Insert product on cart

3. next item on iterator (product 1)

  3.1. Select cart details (which have one element)
  3.2. Insert product on cart

3. end of iterator with 2 products registered on my database.

发生了什么:


1. clean cart
2. enter on iterator (map or something else):
  2.1.a. get the empty cart
  2.1.b. get the empty cart again
  2.2.a. update the product 0 on empty cart selected in 2.1.a. (following the stack of (a)
  2.2.b. update the product 1 on empty cart selected in 2.1.b. (following the stack of (b)

3. Return the cart with only one product inside (the last product).
编辑:我做了一个关于这种行为的视频:

还有一个原始的addProduct函数,它在first wait I put/*…*/上使用异步和并行化在一些地方,让问题变得更小:

  async addProduct(userId, productMarketId, quantity) {
        const productMarketData = await productMarket.findByPk(productMarketId, { /* ... */ });

        const productKey = Buffer.from(PHPSerialize.serialize(`SP_${productMarketId}`)).toString('base64');

        /* ... */

        const cart = await Cart.findOne({ /* ... */ });

        if (!cart) {
            cart = await this.clean(userId);
        }

        /* ... */

        const newCart = await Cart.upsert({ /* ... */ }).then(result => {
            return result.cartUserId;
        });
        return newCart;
    }

这种行为与承诺有关吗?还是异步/等待行为?谢谢。

Array.map不适用于异步函数,因此您应该修改Promise.all调用类似以下内容:

await Promise.all(
    order.OrderProducts.map(data => {
        return CartService.addProduct(order.userId, data.productMarketId, data.quantity);
    })
);

顺序版本


Array.map不适用于异步函数,实际上是这样,但它不能保证承诺是否得到满足或拒绝,它也会像promise.all一样触发所有并行调用,但不能确保所有调用都得到解决。感谢您的解释,但不起作用。同样的行为也发生在不使用async/await的情况下。CartService.addProduct是否支持对同一订单的并行调用?我想您应该按顺序插入产品并使用事务,以便下一个产品添加知道上一个。@Anatoly,购物车同时调用,在这里面,我有两个异步调用,这些调用是并行的,我需要按顺序执行。而且,没有,没有对这些变化的平行支持。
for (const orderProduct of order.OrderProducts) {
  await CartService.addProduct(order.userId, orderProduct.productMarketId, orderProduct.quantity)
}