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

邮差的信 提交于 2019-12-12 17:08:26

问题


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: <6434a712-dbb6-64f0-4b63-62f206f338c4@bens-mbp.lan>
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'});
        }
      });
    });
  }));

回答1:


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.




回答2:


Try to console .log() some values to ensure everything is defined well.

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



回答3:


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.




回答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.




回答5:


Don't forget to use toObject():

var pass = user.toObject().userPass;


来源:https://stackoverflow.com/questions/48240810/iferr-throw-err-error-illegal-arguments-string-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!