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)
compareAPI iscompare(data, encrypted, cb), and there is no example of a call tocomparewith 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...?