Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Error: Illegal arguments: string, undefined" and stop server in node JS

I'm trying to build logging application in node JS. in here password authentication app do not work properly. when i enter username and password it occur following error and stop server.

this is the error.

enter image description here

Here is the code for authentication part

passport.use(new LocalStrategy(
function(username, password, done) {
    User.getUserByUsername(username, function(err, user){
       if(err) throw err;
       if (!user) {
           return done(null, false, {message: 'Unknown user'});
       } 

       User.comparePassword(password, user.password, function(err, isMatch){
           if(err) throw err;
           if (isMatch) {
               return done(null, user);
           } else {
               return done(null, false, {message: 'Invalid password'});
           }
       });
    });
}));

This code work for Unknown user. but it is not working for comparing username and password. i cannot see any bug in here. i want a help for solve this.

like image 700
Buddhika Priyabhashana Avatar asked Jul 02 '17 13:07

Buddhika Priyabhashana


3 Answers

In the name of the universe programmer

in my case i forgot to select the password because in database the password was ((select: false))

this code for app

const user = await User.findOne({email}).select("+password")

i forgot to append the ((.select("+password")))to the findOne

and I received this error ; Error: Illegal arguments: string, undefined

and this code for database

const User = new mongoose.Schema({
    username:{
        type:String,
        required: [true,"نام کاربری ضروری است"]
    },
    email:{
        type:String,
        required: [true,"رایانامه ضروری است"],
        unique: true,
        match:[
            /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{1,3})$/,
            "لطفا یک رایانامه صحیح وارد کنید"
        ]
    },
    password:{
        type:String,
        required:[true,"رمز ضروری است"],
        minlegth: 5,
        select: false
    }
})
like image 84
God is universe programmer Avatar answered Oct 18 '22 05:10

God is universe programmer


I found the problem in here. it is not things regarding the code.

The thing is i had registered two users with same user name and different password. then when i tried to login with the user name and one password it occurred this error and stop the server.

Because there is embarrassing situation with find password to username that user entered. because there are two password with same username.

like image 38
Buddhika Priyabhashana Avatar answered Oct 18 '22 05:10

Buddhika Priyabhashana


In my case, I was using arrow function

userSchema.methods.comparePassword = async (enterdPassword) => {
  return await bcrypt.compare(enterdPassword, this.password);
};

which I converted to normal function

userSchema.methods.comparePassword = async function (enterdPassword) {
  return await bcrypt.compare(enterdPassword, this.password);
};

that solved the problem

like image 27
Siddharth Avatar answered Oct 18 '22 06:10

Siddharth