Ahamed Rishthas Ahamed Rishthas - 2 months ago 12
JSON Question

req.body.data is undefined in expressjs

I am new to nodejs and express js. I am trying to send a json object to my nodejs app using postman. PFB my app code

var express = require("express");
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());

app.post("/game",function(req,res){
console.log(req.body);

console.log(req.body.gameId);
//var body = req.body;
//game.addGame(req,res,body);

});


app.listen(setting.port);
console.log(`The app is listening to port ${setting.port}`);


I send the below json object using postman

{
"gameID": 4,
"gameLevel": 3,
"gameImage": "/3wewe/wewrw",
"gameName": "Game2",
"waitTimeForWinny": 30,
"totalTime": 31,
"orderSequence": 1,
"maxPoints": 10
}


Then I got the below output in the console.

{ gameID: 4,
gameLevel: 3,
gameImage: '/3wewe/wewrw',
gameName: 'Game2',
waitTimeForWinny: 30,
totalTime: 31,
orderSequence: 1,
maxPoints: 10 }
undefined


The json object is coming in the req.body. But I am not able to extract the field from it by useing req.body.gameId

Please help me how to resolve this issue.

Answer

You are passing in the id property as gameID, but you are logging gameId (note the lowercase d)