0
votes

This Error appears when i want to LOG IN

events.js:189

throw err; // Unhandled 'error' event

^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('Incorrect arguments')

at Function.emit (events.js:187:17)

at E:\My Drive\src code\node.js\Revision\regSys\node_modules\mongoose\lib\model.js:4830:13

at E:\My Drive\src code\node.js\Revision\regSys\node_modules\mongoose\lib\query.js:4390:12

at process.nextTick (E:\My Drive\src code\node.js\Revision\regSys\node_modules\mongoose \lib\query.js:2869:28) at process._tickCallback (internal/process/next_tick.js:61:11)

[nodemon] app crashed - waiting for file changes before starting...

Schema (db.js)

const mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');

const stDB = mongoose.Schema({
    email : {
        type: String,
        required: true
    },
    password : {
        type: String,
        required: true
    }
});

stDB.methods.hashPassword = function(password){
    return bcrypt.hashSync(password, bcrypt.genSaltSync(5));
}
stDB.methods.comparePasswords = (password) => {
    return bcrypt.compareSync(password, this.password)
}


module.exports = mongoose.model('db', stDB);

config/passport.js

passport.use('local-signin' , new localStrategy({
    usernameField : 'email' ,
    passwordField : 'password' ,
    passReqToCallback : true ,
} , ( req , email , password ,  done)=>{


    User.findOne({email : email} , (err , user)=>{
        if(err){
            return done (err)
        }

        if (! user){
            return done( null , false , req.flash('signinError' , 'this user not found'))
        }
        if (! user.comparePasswords(password)){
            return done(null , false , req.flash('signinError' , 'wrong password'))
        }

        return done(null , user)
    })

}))

"bcrypt-nodejs": "0.0.3"

"passport": "^0.4.0"

"passport-local": "^1.0.0"

"node": "10.16.1"

"express": "~4.16.1"

1
check if password and email are not null or blank. - Prabhjot Singh Kainth

1 Answers

0
votes

Why you are having the error:
The Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('Incorrect arguments') error is thrown by bcrypt-nodejs when you attempt to compare a hash and string password without passing in the required arguments. This part of your code is the culprit:

stDB.methods.comparePasswords = (password) => {
    return bcrypt.compareSync(password, this.password)
}

Obviously you are passing two arguments to the bcrypt.compareSync, which is what it expects, however, the problem is that you are using an arrow function and mongoose schema methods don't play well with arrow functions, there is this warning note in the docs:

Do not declare methods using ES6 arrow functions (=>). Arrow functions explicitly prevent binding this, so your method will not have access to the document.

So even though it's obvious that you are passing two arguments to bcrypt.compareSync, only one of them is valid, the other one(this.password) would be undefined.

The fix:
Simply replace the arrow function with a function expression:

stDB.methods.comparePasswords = function (password) {
    return bcrypt.compareSync(password, this.password)
}