node.js - 带有 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:这很奇怪,因为它在几个小时前工作
解决方案
推荐阅读
- javascript - 为什么 if 条件语句在 else if(这是错误的)之后终止,尽管在这个 if 条件语句中有另一个 else if?
- reactjs - React 中的导航
- c - int 在内存中的表示
- regex - 使用 Htaccess 重定向 - URL 有下划线并将其更改为斜杠
- shopify - 如何创建 shopify 应用开发环境
- java - Intellij Idea 包装实时模板
- node.js - 节点猫鼬打字稿如何输入 { type: ObjectId, ref: 'User' }
- java - 在Java中查找字符串中单词的最后一秒出现
- java - 如何使用 MD5 spring security 加密密码
- ios - 具有多个单元格的 Collectionview 和 diffable 部分