gpt4 book ai didi

node.js - 未捕获、未指定的 "error"事件

转载 作者:太空宇宙 更新时间:2023-11-03 23:28:12 25 4
gpt4 key购买 nike

我在尝试使用 postman 登录时遇到此错误“未捕获,未指定的“错误”事件。(参数不正确)”。请帮我找出不正确的论点。

这是我的代码:

router.post('/login', function(req, res) {
User.findOne({
username: req.body.username
})
.select('password')
.exec(function(err, user) {

console.log('err', err); // err is null

if (err) throw err;

if (!user) {
res.status(404).send({message: 'User does not exist!'})
}
else if (user) {
var validPassword = user.comparePassword(req.body.password);

if (!validPassword) {
res.status(401).send({message: 'Invalid Password'});
}
else {
var token = createToken(user);
res.json({
success: true,
message: 'Successfully login!',
token: token
})
}
}
})

})

确切的错误:
events.js:165
throw err;
^
Error: Uncaught, unspecified "error" event. (Incorrect arguments)
at Function.emit (events.js:163:17)
at Immediate.<anonymous> (..../node_modules/mongoose/lib/query.js:2322:23)
at runCallback (timers.js:574:20)
at tryOnImmediate (timers.js:554:5)
at processImmediate [as _immediateCallback] (timers.js:533:5)

我正在使用:

  • Node 6.6.0
  • Mongoose 4.7.5
  • MongoDB 2.4.9

最佳答案

找到导致错误的原因,它来 self 的其他代码,这是因为我使用箭头函数而不是匿名函数。

// don't use arrow function if you want to access the this keyword
UserSchema.methods.comparePassword = (password) => {
var user = this;

return bcrypt.compareSync(password, user.password);
}

// use anonymous function instead
UserSchema.methods.comparePassword = function(password) {
var user = this;

return bcrypt.compareSync(password, user.password);
}

关于node.js - 未捕获、未指定的 "error"事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41373710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com