如何解决Sequelize更新问题?

格克图·奥兹莱恩

我使用MySQL ORM。但是更新方法不起作用。

User.update({
  ResetPasswordToken : resetPasswordToken
},{
  where: {
      UserName: 'testuser'
  }
})

顺序记录:

执行(默认):UPDATE UsersSET ResetPasswordToken= ?, updatedAt=?在哪里UserName=?

noob_nerd

根据Sequelize的官方文档,保存方法用于更新实例。请检查页面以获取更多详细信息。

在文档中已经提到了这一点:

如果您更改实例某个字段的值,再次调用save将相应地对其进行更新:


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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章