这是我在github上的回购 https://github.com/code-in-time/node-auth-server-from-udemy
我正在尝试遵循udemy课程
我正试图像这样签名。 (该帐户确实存在)所以它应该工作。
POST /signin HTTP/1.1
Host: localhost:3090
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 338aea0b-3018-9185-00d9-b0affdbfaf69
{
"email": "test@hf.com",
"password": "123"
}
请你能帮我解决这个错误吗?
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
server listening on port, 3090
events.js:137
throw er; // Unhandled 'error' event
^
ReferenceError: isMatch is not defined
at model.userSchema.methods.comparePassword (/home/ddd/Documents/repos/node-auth-server-from-udemy/models/user.js:33:20)
at /home/ddd/Documents/repos/node-auth-server-from-udemy/services/passport.js:17:14
at /home/ddd/Documents/repos/node-auth-server-from-udemy/node_modules/mongoose/lib/model.js:3932:16
at process.nextTick (/home/ddd/Documents/repos/node-auth-server-from-udemy/node_modules/mongoose/lib/query.js:2007:28)
at process._tickCallback (internal/process/next_tick.js:150:11)
[nodemon] app crashed - waiting for file changes before startin
答案 0 :(得分:1)
在这段代码中:
userSchema.methods.comparePassword = function(candidatePassword, callback) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch){
if (err) { throw (err); }
if (err) { return callback(err); }
});
callback(null, isMatch);
}
将回调放在作为第三个参数传递给compare
的函数中。这应该改为
userSchema.methods.comparePassword = function(candidatePassword, callback) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch){
if (err) { throw (err); }
if (err) { return callback(err); }
callback(null, isMatch);
});
}
此外,您应该考虑使用相同的条件合并两个if
语句。实际上,我不确定这些陈述是否符合你真正希望实现的目标。