我尝试创建注册 api,但在 req.body 中遇到了一些问题,当我在此处 console.log 它时,它总是返回为 undefined 我的 controller.js
const{ users: User} = require('../models/index');
const config = require('../config/config');
const jwt = require('jsonwebtoken')
const jwtSignUser = (user) => {
const ONE_WEEK = 60 * 60 * 24 * 7
return jwt.sign(user, config.authentication.jwtSecret, {
expiresIn: ONE_WEEK
})
}
module.exports = {
async register (req, res) {
try{
const user = await User.create(req.body)
const userJson = user.toJSON()
res.send({
user: userJson,
token : jwtSignUser(userJson)
})
}
catch (err){
res.status(400).send({
error:err
})
}
},
}
这里是我的身体解析器声明
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
任何人都可以注意到它有什么问题吗?
确定,添加代码后必须添加路由body-parser
代码
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//Add your route after these lines
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句