使用async / await的bcrypt返回JSON响应
“消息”:“在路径\“ password \”“处,值\” Promise {} \“的字符串转换失败”,
{
"email": "test@t.com",
"pwd": "test123"
}
module.exports = {
postUser: async (req,res,next) =>{
const saltRounds = 10;
const hashPassword = async function(){
const hashPwd = await bcrypt.hash(req.body.pwd,saltRounds);
console.log(hashPwd);
}
hashPassword();
try{
const usr = new User({
email: req.body.email,
password: hashPassword()
})
const user = await usr.save();
console.log(user);
res.status(200).json({
message: 'User created',
user: user
})
}catch(err){
console.log(err);
res.status(500).json({
error: err
})
}
}
}
console.log
确实提供了加密密码
$ 2b $ 10 $ z9cwZ2DtJTcCTDZbVUbCtOEWvImnVRU75PusnNzXq.T2g2zwrOv3i
但是邮递员在以下情况下会出错
{
"email": "test@t.com",
"pwd": "test123"
}
已通过