Reputation: 121
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
= ?
Upvotes: 3
Views: 7936
Reputation: 192
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
Upvotes: 0
Reputation: 551
According 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']});
}
Upvotes: 7