2016-09-28 57 views
0

我是新來的nodejs和表達js。我正嘗試使用郵遞員將json對象發送到我的nodejs應用程序。 PFB我的應用程序代碼req.body.data undefined in expressjs

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}`); 

我使用郵遞員

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

然後我得到了在控制檯下面的輸出發送下面的JSON對象。

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

json對象進入req.body。但我無法通過使用req.body.gameId從中提取字段。請幫助我如何解決此問題。

回答

3

您傳遞id屬性爲gameID,但你正在登錄gameId(注意是小寫d

+0

非常感謝....那是愚蠢的我......有沒有什麼好的IDE的nodejs,這樣我就不必在這個小問題上浪費我的時間。 –

+0

我更喜歡JetBrains [WebStorm](https://www.jetbrains.com/webstorm/)(不是贊助商鏈接,該死的,他們應該給我錢)。我將它用於所有JS/TS開發工作 –