req.body always return as undefined
Clash Royale CLAN TAG#URR8PPP
req.body always return as undefined
i try to create register api but got some problem with req.body thats always return as undefined when i console.log it
here my 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
)
,
here my body parser declaration
app.use(bodyParser.urlencoded( extended: false ));
app.use(bodyParser.json());
anyone can notice whats wrong with it?
i edited my question :)
– Ria Anggraini
Aug 10 at 11:35
1 Answer
1
Make sure, you must add route code after adding body-parser
code
body-parser
app.use(bodyParser.urlencoded( extended: false ));
app.use(bodyParser.json());
//Add your route after these lines
it works thanks :)
– Ria Anggraini
Aug 10 at 11:35
Cheers!!! you can accept the answer if it works for you
– abdulbarik
Aug 10 at 12:02
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please share what you are posting to your api along with the headers
– Guruparan Giritharan
Aug 10 at 11:30