【问题标题】:Sequelize update and deduct multiple rows with different idSequelize更新并扣除不同id的多行
【发布时间】:2019-08-14 13:25:36
【问题描述】:

我有一个产品表,需要一次更新具有不同 ID 的多行。仍然,不知道我该如何实现。

module.exports = sequelize.define('products', {

  product_id: {
    type: Sequelize.INTEGER(20),
  },
  product_quantity: {
    type: Sequelize.INTEGER(225),
  },  
})

var cart_list = [
  { id: 1, curr_quantity: 1 },
  { id: 2, curr_quantity: 4 },
  { id: 3, curr_quantity: 5 },
  { id: 4, curr_quantity: 2 }
]

Product.update({
  product_quantity
})
.then(product => {

})

【问题讨论】:

    标签: mysql orm sequelize.js


    【解决方案1】:

    正如我检查了您的代码,您有 car_list 数组,其中包含多个 id 和不同数量的 b'coz,我们使用循环和承诺。

      module.exports = sequelize.define('products', {    
             product_id: {
                type: Sequelize.INTEGER(20),
             },
             product_quantity: {
                type: Sequelize.INTEGER(225),
             },  
        });
    
        let promises = [];
        var cart_list = [
            { id: 1, curr_quantity: 1 },
            { id: 2, curr_quantity: 4 },
            { id: 3, curr_quantity: 5 },
            { id: 4, curr_quantity: 2 }
        ]
    
        cart_list.forEach(element => {
            promises.push(Product.update({product_quantity : element.curr_quantity},{where:{'product_id' : element.id}}))
        });
    
        Promise.all(promises).then(resultdata=> {
            console.log('Success-------->',resultdata)
        }).catch(err =>{        
            console.log('Failed---------------->',err);
        });
    

    【讨论】:

    • 感谢您的回答,谢谢!它也有效。
    猜你喜欢
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多