为什么我在 bcrypt 比较时出错? async/await 函数
why do i get an error on bcrypt compare? async/await function
我正在尝试使用 nodeJs 和 MySql
登录用户
这里是我连接用户的函数:
export async function connectUser(email, password, cb) {
console.log('Entry connectuser');
let db = dbConnect();
await db.query('SELECT id, email, password FROM user WHERE email = ?', [email], async (error, result) => {
if (error) return cb(error);
const user = result[0];
console.log(result);
console.log(user.email);
console.log(user.password);
console.log(user.id);
const isValid = await bcrypt.compare(password, user.password);
if (isValid === true) {
const token = jwt.sign({id: user.id, email: user.email }, secret, {expiresIn: 86400 });
return cb(null, token);
}
return cb(new Error('Invalid Credentials'));
});
我在 bcrypt.compare()
上收到此错误:“UnhandledPromiseRejectionWarning:错误:非法参数:对象、字符串”
我想这是 async/await 的一个错误,但我无法修复它...一些想法?
如 doc 中所述,您应该这样使用 bcrypt.compare:
bcrypt.compare(req.body.password,user.password, function(err, res) {
if (err){
// handle error
}
if (res)
// Send JWT
} else {
// response is OutgoingMessage object that server response http request
return response.json({success: false, message: 'passwords do not match'});
}
});
我正在尝试使用 nodeJs 和 MySql
登录用户这里是我连接用户的函数:
export async function connectUser(email, password, cb) {
console.log('Entry connectuser');
let db = dbConnect();
await db.query('SELECT id, email, password FROM user WHERE email = ?', [email], async (error, result) => {
if (error) return cb(error);
const user = result[0];
console.log(result);
console.log(user.email);
console.log(user.password);
console.log(user.id);
const isValid = await bcrypt.compare(password, user.password);
if (isValid === true) {
const token = jwt.sign({id: user.id, email: user.email }, secret, {expiresIn: 86400 });
return cb(null, token);
}
return cb(new Error('Invalid Credentials'));
});
我在 bcrypt.compare()
上收到此错误:“UnhandledPromiseRejectionWarning:错误:非法参数:对象、字符串”我想这是 async/await 的一个错误,但我无法修复它...一些想法?
如 doc 中所述,您应该这样使用 bcrypt.compare:
bcrypt.compare(req.body.password,user.password, function(err, res) {
if (err){
// handle error
}
if (res)
// Send JWT
} else {
// response is OutgoingMessage object that server response http request
return response.json({success: false, message: 'passwords do not match'});
}
});