Mongoose: Uncaught, unspecified "error" event. (Incorrect arguments)

Created on 29 Dec 2016  路  7Comments  路  Source: Automattic/mongoose

Hi guys,

I'm encountering this error "Uncaught, unspecified "error" event. (Incorrect arguments)" when trying to login. If this already answered please lead me there.

Here is my code:

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

the exact error:

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)

I'm using:
Node 6.6.0
Mongoose 4.7.5
MongoDB 2.4.9

Most helpful comment

@evilive3000 because of this line -> at Immediate. (..../node_modules/mongoose/lib/query.js:2322:23)

Anyway...

Found what cause the error, it's from my other code and it's because I'm using arrow function instead of anonymous 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);
}

All 7 comments

why did you decide it's related to mongoose?

@evilive3000 because of this line -> at Immediate. (..../node_modules/mongoose/lib/query.js:2322:23)

Anyway...

Found what cause the error, it's from my other code and it's because I'm using arrow function instead of anonymous 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);
}

closing this ticket now.

HI freeman29,
even i used your code i am getting same error

@vetrivivek have you tired using anon function or other function (except arrow function) where ever this keyword is present?

hi guys please am been tormented by same error for days

@Maduflavins Arrow functions seem to cause quite a problem off late

Was this page helpful?
0 / 5 - 0 ratings