if(err) throw err; Error: Illegal arguments: string, undefined

10,370

Solution 1

Had the same issue.

Add console.log() for the passed in values shows they are "undefined".

I tested the endpoint out using Postman to add values to the required field(s), solves the problem.

conclusion: the Password is probably empty. Check if it undefined using console.log(). Then pass some value to it.

Solution 2

In my case the error was -

E:\web\Projects\webapp\auth.js:15
                if(err) throw err;
                        ^

Error: Illegal arguments: number, string
    at _async (E:\web\Projects\webapp\node_modules\bcryptjs\dist\bcrypt.js:286:46)
    at Object.bcrypt.compare (E:\web\Projects\webapp\node_modules\bcryptjs\dist\bcrypt.js:304:13)
    at Promise (E:\web\Projects\webapp\auth.js:11:20)
    at process._tickCallback (internal/process/next_tick.js:68:7)
[nodemon] app crashed - waiting for file changes before starting...

It was because the password was in number and it was throwing an error! I just converted the entered password into string!

Code Before Solving Error-

bcrypt.compare(password.toString(), user.password, (err, isMatch)=>{
                if(err) throw err;
                if(isMatch){
                    resolve(user);
                }else{
                    //Password Wrong
                    reject("Auth Failed");
                }
            });

After Solving Error- What I did is, I added .toString() function in the password argument to convert it into string!

bcrypt.compare(password.toString(), user.password, (err, isMatch)=>{
                if(err) throw err;
                if(isMatch){
                    resolve(user);
                }else{
                    //Password Wrong
                    reject("Auth Failed");
                }
            });

I hope it helps someone.

Solution 3

You might have set the select property to false on password. And this is causing the error. The mongoose is not returning the password because you have set the select to false. You now need to explicitly ask for the password when querying. Use the select method where you are querying for the user and pass the +password as a string like this.

.select('+password')

Solution 4

Error: Illegal arguments: string, undefined could mean either your hash value or user input is not recognized a string by the compiler. First try to log your input together with what your user(from database is returning), if your user.password does not return a string then you should check your Schema if it was predefined a string and make sure you don't use select: false in your password, it may restrict query.

Share:
10,370
Admin
Author by

Admin

Updated on August 21, 2022

Comments

  • Admin
    Admin over 1 year

    I now have

    module.exports.comparePassword = function(candidatePassword, hash, callback) {
      console.log(candidatePassword)
     console.log(hash)
      bcrypt.compare(candidatePassword, hash, function(err, ismatch) {
        if(err) throw err;
        callback(null, ismatch);
      });
    }
    

    and the logs now are

    Server is up on port 3000
    Connection has been established
    Sat Jan 13 2018 14:45:36 GMT+0000 (GMT): GET /users/login
    Sat Jan 13 2018 14:45:42 GMT+0000 (GMT): POST /users/login
    testing1234
    undefined
    /Users/benbagley/Code/poetry-out-loud/models/user.js:101
        if(err) throw err;
                ^
    
    Error: Illegal arguments: string, undefined
        at _async (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:286:46)
        at Object.bcrypt.compare (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:304:13)
        at Function.module.exports.comparePassword (/Users/benbagley/Code/poetry-out-loud/models/user.js:100:10)
        at /Users/benbagley/Code/poetry-out-loud/routes/users.js:176:12
        at model.Query.<anonymous> (/Users/benbagley/Code/poetry-out-loud/node_modules/mongoose/lib/model.js:4056:16)
        at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:273:21
        at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:131:16
        at process._tickCallback (internal/process/next_tick.js:150:11)
    

    It seems here the password is getting shown but not the hash.

    Original

    Hi I'm getting the following error, not sure what's causing it

    Message sent: <[email protected]>
    Preview URL: https://ethereal.email/message/WlVWjq0qIgpSmhJbWloWhUGTHAp3fWC4AAAAbOQTYPu-4HjQWkI0i1uv5Ds
    Sat Jan 13 2018 14:24:05 GMT+0000 (GMT): GET /users/login
    Sat Jan 13 2018 14:24:24 GMT+0000 (GMT): POST /users/login
    /Users/benbagley/Code/poetry-out-loud/models/user.js:99
        if(err) throw err;
                ^
    
    Error: Illegal arguments: string, undefined
        at _async (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:286:46)
        at Object.bcrypt.compare (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:304:13)
        at Function.module.exports.comparePassword (/Users/benbagley/Code/poetry-out-loud/models/user.js:98:10)
        at /Users/benbagley/Code/poetry-out-loud/routes/users.js:176:12
        at model.Query.<anonymous> (/Users/benbagley/Code/poetry-out-loud/node_modules/mongoose/lib/model.js:4056:16)
        at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:273:21
        at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:131:16
    

    The users are being created I just can't sign in.

    Here is the lines causing the error

    module.exports.comparePassword = function(candidatePassword, hash, callback) {
      bcrypt.compare(candidatePassword, hash, function(err, ismatch) {
        if(err) throw err;
        callback(null, ismatch);
      });
    }
    

    here is the passport implementation

    passport.use(new LocalStrategy({
      usernameField: 'email'
      },
      function(email, password, done) {
        User.getUserByEmail(email, function(err, user){
          if(err) throw err;
          if(!user){
            return done(null, false, {message: 'Unknown Email Address'});
          }
    
          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'});
            }
          });
        });
      }));