日期字段未使用.updateMany()更新-猫鼬

时间:2019-12-11 00:31:47

标签: node.js mongodb express mongoose

我正在更新我的应用程序的密码。密码哈希并更新正常。我得到nModified:1.我注销并可以使用新密码登录,但是date字段从未更新?我如何也可以更新该字段?

router.put("/password/:id", (req, res) => {
  let user = req.body;
  let retrievedOldPassword = user.retrievedOldPassword.password;
  let result;
  let newPasswordHash;

  bcrypt.compare(req.body.oldPassword, retrievedOldPassword, function(
    err,
    res
  ) {
    if (err) {
      return err;
    }
    result = res;
    console.log(res);
  });

  if (result) {
  } else {
    bcrypt.hash(req.body.newPassword, 10, function(err, hash) {
      newPasswordHash = hash;
      // res.sendStatus(401);

      User.updateMany(
        { password: retrievedOldPassword },
        { password: newPasswordHash },
        { passwordCreated: Date.now() }
      )
        .then(user => {
          console.log(user);
          res.json(user);
        })
        .catch(err => console.log(err));
    });
  }
});

1 个答案:

答案 0 :(得分:1)

updateMany()的第三个参数用于选项。您需要将passwordCreated: Date.now()放在第二个参数中,然后它将被更新:

User.updateMany(
    { password: retrievedOldPassword },
    { password: newPasswordHash, passwordCreated: Date.now() },
  )

更多信息:here