这个错误通常出现在使用 Bcrypt 模块时,因为参数回调传递的是一个字符串而不是一个函数。解决这个问题的方法是确保第二个参数的类型是一个函数。
这里是一个示例代码,可以解决这个问题:
const bcrypt = require('bcrypt');
const saltRounds = 10;
const myPlaintextPassword = 'mypassword';
const someOtherPlaintextPassword = 'notmypassword';
bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) {
// Store hash in your password DB.
if (err) {
console.log(err);
} else {
bcrypt.compare(myPlaintextPassword, hash, function(err, res) {
// res == true
console.log(res);
});
bcrypt.compare(someOtherPlaintextPassword, hash, function(err, res) {
// res == false
console.log(res);
});
}
});
注意到这里第二个参数传递的是一个变量 saltRounds,而不是一个字符串。如果你传递一个字符串类型参数,Bcrypt 模块会报错,并提示“Error: Illegal callback: string”。