首页 > 解决方案 > 带有 express api 的空正文在 bcryptjs 时引发错误

问题描述

我的目标是使用 express api(与 angular 前端一起使用)制作一个身份验证系统 几个小时前,我在创建前端时遇到了一个问题:api 在代码中对密码进行哈希处理时引发了一些错误:

            const name = req.body.name;
            const email = req.body.email;
            const password = bcrypt.hashSync(String(req.body.password));
            console.log(req.body);
            usermodel.createUser([name, email, password], (err) => {
                try {
                    if (err) return res.json({
                        success: false,
                        message: err.toString(),
                    });
                    usermodel.findUserByEmail(email, (err, user) => {
                        if (err) return res.json({
                            success: false,
                            message: 'Server error! '
                        });
                        console.log(user);
                        const expiresIn = 24 * 60 * 60;
                        const accessToken = jwt.sign({
                            id: user.id
                        }, config.secret, {
                            expiresIn: expiresIn
                        });
                        res.json({
                            success: true,
                            "user": user,
                            "access_token": accessToken,
                            "expires_in": expiresIn
                        });
                    });
                } catch (error) {
                    console.log(error);
                    res.json({success: false, message:error.toString()})
                }

经过一些修改,即使我输入了一些参数,主体似乎也是空的:

app.post('/ping', function(req, res) {
  console.log('trigerred')
  res.json({body: req.body});
});

--> undefined :( 我已经遇到了问题,这是因为我的 express api 不是带有邮递员的 https,所以我将其设为 https。我验证了 bodyparser:

  app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

知道为什么身体是空的吗?谢谢^^ PS:这很奇怪,因为它在几个小时前工作

标签: node.jsapiexpressbody-parser

解决方案


推荐阅读