admin管理员组文章数量:1355553
I use MySQL ORM. But update method not working.
User.update({
ResetPasswordToken : resetPasswordToken
},{
where: {
UserName: 'testuser'
}
})
Sequelize Log:
Executing (default): UPDATE Users
SET ResetPasswordToken
=?,updatedAt
=? WHERE UserName
= ?
I use MySQL ORM. But update method not working.
User.update({
ResetPasswordToken : resetPasswordToken
},{
where: {
UserName: 'testuser'
}
})
Sequelize Log:
Executing (default): UPDATE Users
SET ResetPasswordToken
=?,updatedAt
=? WHERE UserName
= ?
2 Answers
Reset to default 7According to the official documentation of Sequelize, save method is used for updating an instance. Please check this page for more detail.
This has been mentioned in the documentation:
If you change the value of some field of an instance, calling save again will update it accordingly:
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!
Similarly, your code can be written as:
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']});
}
You can update several fields at once with the set method:
const jane = await User.create({ name: "Jane" });
jane.set({
name: "Ada",
favoriteColor: "blue"
});
// As above, the database still has "Jane" and "green"
await jane.save();
// The database now has "Ada" and "blue" for name and favorite color
本文标签: javascriptHow to fix Sequelize update issueStack Overflow
版权声明:本文标题:javascript - How to fix Sequelize update issue? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743985533a2571260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论