在使用bcryptwith async / await

时间:2019-07-12 07:02:02

标签: express async-await bcrypt

使用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"
}

已通过

0 个答案:

没有答案