【发布时间】:2018-11-29 22:41:57
【问题描述】:
试图找出在 expressjs 中使用 Sequelize 事务的正确方法。 以下代码通过 id 查找帖子,增加帖子表中的回复计数,更新帖子,然后在回复表中创建回复。尝试在控制器操作中调用代码但它不起作用。内部承诺没有受到打击。关于在 Sequelize 中使用交易的正确方法有什么建议吗?
Sequelize.transaction(function (t) {
db.Post.find({where:{id: postId}}, {transaction:t}).then(function(post)
{
var count = post.reply + 1;
post.update({replyCount:count},{transaction:t});
db.Reply.create(replyData, {transaction:t}).then(function(newcomment, created){
res.json(newcomment);
});
});
}).then(function (result) {
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback
}).catch(function (err) {
// Transaction has been rolled back
// err is whatever rejected the promise chain returned to the transaction
});
});
【问题讨论】:
标签: node.js express transactions sequelize.js