首页 > 解决方案 > 使用 node.js 从 mongodb 更改用户密码

问题描述

我试图在我的应用程序中实现一种方法来更改登录用户的密码。我正在使用 node.js 和 mongodb。刚在mongodb官方博客上直接找到示例代码:

https://www.mongodb.com/blog/post/password-authentication-with-mongoose-part-1

 User.findOne(req.firstname, function (err, user) {
   if (err) throw err;

   user.comparePassword(req.password, function (err, isMatch) {
     if (err) throw err;
     console.log(isMatch); // -> Returns True if match
  });
 });      
}); 

在我的用户模型中,我有以下代码:

   userSchema.methods.comparePassword = function(candidatePassword, cb) {
     console.log("candidatepw-> " + candidatePassword);
     console.log("password->" + this.password);
    bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
        if (err) return cb(err);
        cb(null, isMatch);
    });
  };

当我看到console.log 输出时,我意识到变量candidatePassword 是未定义的。那个怎么样?我错过了什么吗?我该如何解决?

标签: node.jsmongodbexpressmongoose

解决方案


推荐阅读