[英]Nodejs Sequelize transaction using async/await not rolling back
let transaction;
try {
transaction = await connection.sequelize.transaction();
const employee = await Employee.findOne({
where: {
uuid: req.params.uuid
}
});
if (employee) {
await Account.destroy( {
where: {
uuid: employee.accountUuid
}
}, {transaction});
await Employee.destroy({
where: {
uuid: req.params.uuid
}
}, {transaction});
await transaction.commit();
return res.status(200).json({
messages: messages.MSG_SUCCESS
});
} else {
return res.status(404).json({
message: constants.EMPLOYEE + messages.MSG_NOT_FOUND
});
}
} catch (error) {
if (transaction) await transaction.rollback();
return res.status(500).json({
message: messages.MSG_CANNOT_DELETE + constants.EMPLOYEE
});
}
我使用這段代碼來創建交易。 它工作正常,直到員工刪除失敗。 它確實會在 console.log 中調用回滾,但該帳戶仍會在數據庫中被刪除。
事務應在與 object 相同where
object 中傳遞。
await Account.destroy({
where: {
uuid: employee.accountUuid
},
transaction
});
參考: 續集銷毀
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.