How to fix Sequelize update issue?

Göktuğ Ozleyen

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 = ?

noob_nerd

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']});
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related