gpt4 book ai didi

javascript - 序列化事务批量更新,然后批量创建

转载 作者:行者123 更新时间:2023-11-29 06:59:47 24 4
gpt4 key购买 nike

我正在使用 Sequelize 事务,并且想知道如何在顺序执行批量创建之前执行批量更新。

我当前的代码是这样的:

return sequelize.transaction(function(t){
return sequelize.Promise.each(arrToUpdate, function(itemToUpdate){
model.update(itemToUpdate, { transaction: t })
});
//How to do a sequential bulk create after the bulk update is successful in sequelize
//transaction?
//Bulk Create code would be return model.bulkCreate(itemsArray, { transaction: t })
});

最佳答案

我相信您只是在使用 then 进行 promise 链接?第一行应该返回一个 promise - 因此只需对结果调用 then 即可:

return sequelize.transaction(function(t){
return sequelize.Promise.each(arrToUpdate, function(itemToUpdate){
model.update(itemToUpdate, { transaction: t })
}).then((updateResult) => {
return model.bulkCreate(itemsArray, { transaction: t })
}, (err) => {
// if update throws an error, handle it here.
});
});

注意:现在您的函数将返回一个 promise ,因此无论调用您的函数,都必须使用 then 来获取结果的句柄。

关于javascript - 序列化事务批量更新,然后批量创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43967364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com