I'm having issue with the request body being undefined even after using the body-parser. I'm using prefixed routing also.
//adding router
const route = require('./routes/route')
app.use('/api',route)
//body-parser
app.use(bodyParser.json())
This is the issue.
TypeError: Cannot read property 'firstName' of undefined
at E:\Projects\MEAN\routes\route.js:24:29
at Layer.handle [as handle_request] (E:\Projects\MEAN\node_modules\express\lib\router\layer.js:95:5)
at next (E:\Projects\MEAN\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\Projects\MEAN\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\Projects\MEAN\node_modules\express\lib\router\layer.js:95:5)
at E:\Projects\MEAN\node_modules\express\lib\router\index.js:281:22
at Function.process_params (E:\Projects\MEAN\node_modules\express\lib\router\index.js:335:12)
at next (E:\Projects\MEAN\node_modules\express\lib\router\index.js:275:10)
at Function.handle (E:\Projects\MEAN\node_modules\express\lib\router\index.js:174:3)
at router (E:\Projects\MEAN\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (E:\Projects\MEAN\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (E:\Projects\MEAN\node_modules\express\lib\router\index.js:317:13)
at E:\Projects\MEAN\node_modules\express\lib\router\index.js:284:7
at Function.process_params (E:\Projects\MEAN\node_modules\express\lib\router\index.js:335:12)
at next (E:\Projects\MEAN\node_modules\express\lib\router\index.js:275:10)
at expressInit (E:\Projects\MEAN\node_modules\express\lib\middleware\init.js:40:5)
The issue is caused when I used router config above the body-parser config
like this
//adding router
const route = require('./routes/route')
app.use('/api',route)
//body-parser
app.use(bodyParser.json())
but simply changing the order solved my issue. like this,
//body-parser
app.use(bodyParser.json())
//adding router
const route = require('./routes/route')
app.use('/api',route)