node.js - 使用 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 是未定义的。那个怎么样?我错过了什么吗?我该如何解决?
解决方案
推荐阅读
- c# - 键值对 C#
- python - Django ORM - 带有两列的左外连接?
- asp.net-mvc - 在 mvc 中下载文件时出现异常
- email - 跟踪最后一封电子邮件的正确方法是什么?
- r - facet_rep_wrap repeat.tick.labels=F 仍然显示所有轴
- c# - 使用 C# 反射查找声明函数的接口
- python - 从嵌套字典开发 matplotlib 表面
- python - 向数据框添加一列重复值
- javascript - JavaScript Regex:匹配所有主题标签,仅捕获哈希字符后的文本
- wpf - WPF:通过 MemoryStream 使用 ImageMagick 显示 gif