0

I have this error when I try to test my API to change password. How can I fix this error?

Controller:

exports.changePassword = (req, res) => {
  try {
    const user = User.findByPk(req.params.user_id);
    var body = req.body;
    if (!user) {
      return res.status(400).send("invalid value");
    }
    
    const salt = bcrypt.genSaltSync(10);
    const newPassword = bcrypt.hashSync(body.newPassword, salt);
    bcrypt.compare(
      body.password,
      user.password,
      salt,
      async function (err, isMatch) {
        if (err) {
          throw err;
        }
        if (!isMatch) throw new Error("Password not matched!");

      

        user.set(
          { password: newPassword, updated_at: now() },
          {
            where: {
              user_id: user.user_id,
            },
          }
        );

        await user.save();
      }
    );
    res.status(200).send("Password Changed successfully!");
  } catch (error) {
    res.send("An error occured");
    console.log(error);
  }
};

Here is the error:

Error: Illegal callback: string
    at Object.bcrypt.compare (C:\Users\admin\Desktop\Local Project\node_modules\bcryptjs\dist\bcrypt.js:303:23)
    at exports.changePassword (C:\Users\admin\Desktop\Local Project\controllers\user.controller.js:146:12)
    at Layer.handle [as handle_request] (C:\Users\admin\Desktop\Local Project\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\admin\Desktop\Local Project\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (C:\Users\admin\Desktop\Local Project\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\Users\admin\Desktop\Local Project\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\admin\Desktop\Local Project\node_modules\express\lib\router\index.js:284:15
    at param (C:\Users\admin\Desktop\Local Project\node_modules\express\lib\router\index.js:365:14)
    at param (C:\Users\admin\Desktop\Local Project\node_modules\express\lib\router\index.js:376:14)
    at Function.process_params (C:\Users\admin\Desktop\Local Project\node_modules\express\lib\router\index.js:421:3)
4
  • 3
    Looking at the API documentation, it clearly says the compare API is compare(data, encrypted, cb), and there is no example of a call to compare with four arguments. And the third argument, when used, is always a callback in the documentation -- which matches the described API. But you're providing a string as the third argument, and the error message is complaining that the callback you've provided is a string. Seems fairly clear what the error is...? Commented Sep 26, 2022 at 7:53
  • So, how to fix it, could you help me ? Commented Sep 26, 2022 at 7:58
  • 2
    You need to just pass the right arguments to compare function, you've got one too much I guess Commented Sep 26, 2022 at 8:00
  • Help me man, how can i fix this ? Commented Sep 26, 2022 at 8:04

1 Answer 1

1

I don't think compare function needs salt. Just try to use it like this:

bcrypt.compare(body.password, user.password, (error, isMatch) => {
...
})
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah you right, i fixed it here:stackoverflow.com/questions/73849484/…. I hope you can upvote for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.