未捕获,未指定的“错误”事件

我遇到这个错误“Uncaught,unspecified”错误“事件。(不正确的参数)”当试图login使用邮递员。 请帮我弄清楚不正确的论点。

这是我的代码:

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) 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)

我在用着:

  • 节点6.6.0
  • mongoose4.7.5
  • MongoDB 2.4.9

       

网上收集的解决方案 "未捕获,未指定的“错误”事件"

发现是什么原因引起的错误,这是从我的其他代码,这是因为我使用箭头function,而不是匿名函数。

 // 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); }