我使用 MySQL ORM。但是更新方法不起作用。
User.update({
ResetPasswordToken : resetPasswordToken
},{
where: {
UserName: 'testuser'
}
})
Sequelize 日志:
正在执行(默认):UPDATE Users
SET ResetPasswordToken
=?,updatedAt
=? WHERE 用户名
= ?
最佳答案
根据Sequelize的官方文档,save方法用于更新一个实例。请查看this页面了解更多详情。
文档中已经提到了这一点:
如果您更改实例的某些字段的值,再次调用保存将相应地更新它:
const jane = await User.create({ name: "Jane" });
console.log(jane.name); // "Jane"
jane.name = "Ada";
// the name is still "Jane" in the database
await jane.save();
// Now the name was updated to "Ada" in the database!
同样,你的代码可以写成:
const foo = async (resetPasswordToken) => {
//Finding current instance of the user
const currentUser = await User.findOne({
where:{
UserName: 'testuser'
}
});
//modifying the related field
currentUser.ResetPasswordToken = resetPasswordToken;
//saving the changes
currentUser.save({fields: ['ResetPasswordToken']});
}
关于javascript - 如何解决 Sequelize 更新问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65671048/