reactjs - 更新用户信息 sequelize express

标签 reactjs express sequelize.js

我正在尝试更新用户信息。用户创建的帐户只有名称电子邮件和密码,我正在尝试使用其他可选字段更新用户信息,如果不使用数据库中的旧密码,我如何检查密码是否已更改更新密码。我在我的前端使用 react。我正在使用 sequilize orm - mysql。我怎么能做到这一点。当我什至尝试用新密码覆盖密码时,它给了我一个错误。请帮助。谢谢

  router.post("/update", (req, res) => {
  const dataToday = new Date();
  const studentData = {
    name: req.body.name,
    email: req.body.email,
    password: req.body.password,
    phoneNumber: req.body.phoneNumber,
    about_me: req.body.about_me,
    city: req.body.city,
    country: req.body.country,
    company: req.body.company,
    school: req.body.school,
    hometown: req.body.hometown,
    gender: req.body.gender,
    created: dataToday
  };
  console.log(studentData);

  Student_Info.findOne({
    where: {
      email: req.body.email
    }
  })
    .then(student => {
      if (student) {
        bcrypt.hash(req.body.password, 10, (err, hash) => {
          studentData.password = hash;
          Student_Info.update(studentData)
            .then(student => {
              res.json({
                status: student.email + "updated"
              });
            })
            .catch(err => {
              res.send("error" + err);
            });
        });
      } else {
        res.json({
          error: "No User Found"
        });
      }
    })
    .catch(err => {
      res.send("error" + err);
    });
});

最佳答案

尝试使用 compareSync 怎么样?

Student_Info.findOne({
  where: {
    email: req.body.email
  }
})
  .then(student => {
    if (!student) throw new Error("No User Found")

    // return true if old password equals new password
    const validPassword = bcrypt.compareSync(req.body.password, student.password)
    if (validPassword) throw new Error("Same Password")
    studentData.password = bcrypt.hashSync(req.body.password, 10)
    return student.update(studentData)
  })
  .then(student => {
    res.json({
      status: student.email + "updated"
    });
  })
  .catch(err => {
    res.send("error" + err);
  });

关于reactjs - 更新用户信息 sequelize express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55120229/

相关文章:

html - 使用 react-bootstrap-table - 将搜索栏放在左侧

javascript - 将 req.query ['name' ] 的值保存到 Node.jS 中的变量中?

mysql - 如何在nodejs回调函数中从mysql获取完整数据

node.js - 有没有办法在 Sequelize 的 isAlpha 验证中包含特殊字符?

reactjs - 为什么jsx末尾不需要加分号?

html - 为什么我在 material -ui-next "Cards"示例中看不到图像?

error-handling - 父类(super class)构造函数调用应该在构造函数主体中

javascript - 在expressjs和JADE环境中可视化数据

postgresql - Sequelize Migration addIndex 不按降序添加索引

mysql - 使用环境变量进行本地 Sequelize 配置