Ahamed Rishthas Ahamed Rishthas - 1 year ago 137
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();


//var body = req.body;


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 }

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download