【发布时间】:2023-03-30 22:44:01
【问题描述】:
我正在使用成功返回的 Sequelize 事务,但是结果未定义。如文档中所述:
.then(function (result) {
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback
结果应该包含东西!
我的代码工作并成功更新数据库,并进入.then 块,但结果日志为“未定义”。
return sequelize.transaction(t => {
return createUser(body, {transaction: t})
.then(user => {
if (user != null) {
return createSetup({"user_id" : user.dataValues.id}, {transaction: t})
.then(setup => {
console.log("setup was created")
})
.catch(err => {
throw new Error("the setup key was not created successfully... reverting transaction 2")
})
}
else {
throw new Error("no return value from creating user");
}
})
.catch(err => {
throw new Error("Failed to create user")
})
})
.then(result => {
console.log(result) //this returns undefined
res.status(200).send({
success: true,
message: "The user was successfully created.",
})
})
.catch(error => {
console.log("error")
res.status(400).send({
success: false,
message: error
})
})
})
我真的想要user 对象的结果,但无论我做什么,它仍然是未定义的。我已经尝试过交易中的后续函数有无返回。
【问题讨论】:
标签: node.js sql-server transactions sequelize.js