[英]Sequelize doesn't update after using 'findOne' or 'findAll'
我遇到了这个问题,我尝试从 dB 更新找到的行。 我为这个 v5 使用了 sequelize。 问题是在找到我想要的行之后,我使用了很多语法,但都没有奏效。 我尝试了 instance.update({thing: value})、instance.save({thing: value})、model.update 等。它不会返回任何类型的错误或警告,但不会更新任何内容。
const t = await sequelize.transaction();
try{
const foundUser = await User.User.findOne({
where: {
userName: req.body.userName,
password: req.body.password
}
}, {transaction: t });
await userFunction.validateLogin(foundUser);
foundUser.tooken = '10:10:10';
await foundUser.save();
await t.commit();
这里只是返回找到的行,它不会更新任何内容。 这是返回的内容:
Executing (60652c1d-4e58-449e-bde1-3ae7512651ac): START TRANSACTION;
Executing (default): SELECT "id", "firstName", "lastName", "userName", "password", "email", "birthDay", "createdAt", "updatedAt" FROM "users" AS "user" WHERE "user"."userName" = 'dudu3000' AND "user"."password" = 'admin' LIMIT 1 FOR UPDATE;
Executing (60652c1d-4e58-449e-bde1-3ae7512651ac): COMMIT;
您可以在findOne
方法的.then
中调用 update 方法
SequlizeModel.findOne({
where: {
userName: req.body.userName,
password: req.body.password
}
})
.then(record => {
if (!record) {
throw new Error('No record found')
}
console.log(`retrieved record ${JSON.stringify(record,null,2)}`)
record.update(values).then( updatedRecord => {
console.log(`updated record ${JSON.stringify(updatedRecord,null,2)}`)
// login into your DB and confirm update
})
})
.catch((error) => {
// do seomthing with the error
throw new Error(error)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.