提交的 Sequelize 事务的结果是 "undefined"
Result of committed Sequelize transaction is "undefined"
我正在使用 returns 成功的 Sequelize 事务,但是,结果未定义。如文档中所述:
.then(function (result) {
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback
结果应该包含内容!
我的代码成功运行并更新了数据库,并进入了 .then
块,但结果记录为 "undefined"。
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
对象的结果,但无论我做什么,它仍然是未定义的。我在交易中的后续功能上尝试了 returns 和 returns。
试试这个
.then(setup => {
return { user, setup }
})
我假设
return createUser(body, {transaction: t})
将是“承诺链的结果 returned 到事务回调
但是,我还需要 return 用户对象才能在 result
中获取它
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 => {
//this line is necessary to get the value in the result of the transaction
return user, setup
})
我正在使用 returns 成功的 Sequelize 事务,但是,结果未定义。如文档中所述:
.then(function (result) {
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback
结果应该包含内容!
我的代码成功运行并更新了数据库,并进入了 .then
块,但结果记录为 "undefined"。
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
对象的结果,但无论我做什么,它仍然是未定义的。我在交易中的后续功能上尝试了 returns 和 returns。
试试这个
.then(setup => {
return { user, setup }
})
我假设
return createUser(body, {transaction: t})
将是“承诺链的结果 returned 到事务回调
但是,我还需要 return 用户对象才能在 result
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 => {
//this line is necessary to get the value in the result of the transaction
return user, setup
})