2015-06-21 35 views
0

試圖獲得一個職位工作,基本上它的用戶配置文件,整個對象被髮送到一個角度形式從快遞(存儲在mongo),但是,我無法獲得讀取我發回的數據(使用body parser)。快速發佈不讀取請求有效負載

首先,發送部分是採用了棱角分明一個帖子:

$scope.saveProfile = function(empty) { 
    $scope.user.updateMe = false; 
    $http({ 
     method: 'POST', 
     url: '/api/postProfile/'+$scope.user._id, 
     data: $scope.user 
    }) 
     .success(function(data, status, headers, config) { 
      console.log("I updated"); 
     }) 
     .error(function(data, status, headers, config) { 
      console.log("Big fat fail that one"); 
     }); 

    } 

這給了我下面的帖子:

 { 
     "startedDateTime": "2015-06-21T06:53:50.363Z", 
     "time": 0, 
     "request": { 
      "method": "POST", 
      "url": "http://localhost:29771/api/postProfile/55841857579127018071ad97", 
      "httpVersion": "unknown", 
      "headers": [ 
      { 
       "name": "Accept", 
       "value": "application/json, text/plain, */*" 
      }, 
      { 
       "name": "Referer", 
       "value": "http://localhost:29771/internal/profile" 
      }, 
      { 
       "name": "Origin", 
       "value": "http://localhost:29771" 
      }, 
      { 
       "name": "User-Agent", 
       "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36" 
      }, 
      { 
       "name": "Content-Type", 
       "value": "application/json;charset=UTF-8" 
      } 
      ], 
      "queryString": [], 
      "cookies": [], 
      "headersSize": -1, 
      "bodySize": 448, 
      "postData": { 
      "mimeType": "application/json;charset=UTF-8", 
      "text": "{\"_id\":\"55841857579127018071ad97\",\"updateMe\":false,\"imageUrl\":\"https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50\",\"familyName\":\"Bberg\",\"firstName\":\"Ben\",\"userName\":\"Ben Bberg\",\"__v\":0,\"google\":{\"email\":\"[email protected]\",\"name\":\"Ben Bberg\",\"token\":\"ya29.lwGTHvRHvXhnBGTYSFVztPrjlkvtq-0HenpocX0VCsUmnbMt5zpluo411-0nyCqH2xgfwO4YCTuRyA\",\"id\":\"108193570873442725868\"}}" 
      } 
     }, 
     "response": { 
      "status": 0, 
      "statusText": "", 
      "httpVersion": "unknown", 
      "headers": [], 
      "cookies": [], 
      "content": { 
      "size": 0, 
      "mimeType": "x-unknown" 
      }, 
      "redirectURL": "", 
      "headersSize": -1, 
      "bodySize": -1, 
      "_transferSize": 0, 
      "_error": "net::ERR_EMPTY_RESPONSE" 
     }, 
     "cache": {}, 
     "timings": { 
      "blocked": -1, 
      "dns": -1, 
      "connect": -1, 
      "send": 0, 
      "wait": 0, 
      "receive": 0, 
      "ssl": -1 
     }, 
     "pageref": "page_8" 
     } 
    ] 
    } 
} 

然而,當我嘗試用身體下面的進行urlencode路由器

app.post('/api/postProfile/:id', isLoggedIn, urlencodedParser, function(req, res) { 
    console.log("I got called here"); 
    var id = req.params.id; 
     console.log("I will update " + id + " with : " + req.body); 
     for (var key in req.body){ 
      console.log(key + " value: " + req.body[key]); 
     } 
}); 

我什麼也沒得到,可以看到我嘗試通過對象req.body鍵,但沒有什麼似乎,下面是輸出日誌

POST /api/postProfile/55841857579127018071ad97 - - ms - - 
I got called here 
I will update 55841857579127018071ad97 with : [object Object] 

任何人有任何建議嗎?

回答

0

經過大量的fa I之後,我發現了這個問題。唯一缺少的路線是:

app.use(bodyParser.json())

的問題似乎是,如果你錯過了這條線,你會得到一個完全空req.body,但是,一旦我有這一切都完美的作品。 希望這可以幫助別人:)