gpt4 book ai didi

node.js - ExpressJS 中的 Sequelize 交易未执行

转载 作者:太空宇宙 更新时间:2023-11-03 23:19:59 27 4
gpt4 key购买 nike

尝试找出在expressjs中使用Sequelize事务的正确方法。以下代码通过 id 查找帖子,增加帖子表中的回复计数,更新帖子,然后在回复表中创建回复。尝试在 Controller 操作中调用代码,但它不起作用。内部 promise 没有兑现。关于 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
});
});

最佳答案

Sequelize.transaction(function (t) {  
return db.Post.find({where:{id: postId}}, {transaction:t}).then(function(post)
{
var count = post.reply + 1;
return post.update({replyCount:count},{transaction:t})
.success(result =>
// i guess you want to use result in here to create replyData
return db.Reply.create(replyData, {transaction:t})

.then(function(newcomment, created){
return res.json(newcomment);

});
);

});

关于node.js - ExpressJS 中的 Sequelize 交易未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50954296/

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