我正在使用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
链接?第一行应该返回一个承诺 - 所以只需在结果上调用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.
});
});
注意:现在你的函数将返回一个承诺,所以无论调用什么,你的函数都必须使用then
来获取结果的句柄。