为什么req./body.param通过GET请求未定义?

时间:2016-06-05 17:35:07

标签: node.js express mongoose body-parser npm-request

我的问题是req.body.param未定义,但正文本身从请求的源返回所需的json。

这意味着我的迭代也不会工作,因为我需要访问req.body.data.length或可用的id参数的计数。 这也是为什么我将num次迭代设置为5以测试输出。

项目是通过快速生成器创建的(安装了body-parser)。

这就是我得到的回复

[
 {
  __v: 0,
  id: "0",
  appid: 730,
  updated_at: "2016-06-05T16:52:39.742Z",
  _id: "575458d71d1c41f414e3b29a",
  created_at: "2016-06-05T16:52:39.731Z"
 },
 {
 __v: 0,
 id: "1",
 appid: 730,
 updated_at: "2016-06-05T16:52:39.749Z",
 _id: "575458d71d1c41f414e3b29b",
 created_at: "2016-06-05T16:52:39.737Z"
 },  ... 
]  

这就是请求的api json正文的样子:

{
 data: [
        {
          id: 1,
          product_name: "Product1",
          app: appName,
          quantity: 137360,
          price: 0.05,
          updated_at: "2016-06-04 23:02:03",
          median_week: 0.04,
          median_month: 0.05,
         },
         {
          id:2,
          ...,
         }   
         .....
         {
          id:142640,
          ...,
         }   
       }  

我的功能

router.get('/', function(req, res) {
 request.get('url/api/items/all/', function (error,  response, body) {
console.log(options);
console.log('###########\n '+ body + '\n');
console.log('error '+error);
console.log('req:body '+req.body.data);
if (error && response.statusCode !== 200) {
    // request could not be completed
    return res.send(400, {message: 'Something went wrong'});
} else {
   // get the count
   //console.log('body.id.length: '+req.body.id.length);
   //console.log('body.data.length: '+req.body.data.length);
   //console.log('data.length: '+data.length);

   var iterator = 5;//req.body.data.length; //|| req.body.num_items;
   // iterate number of times
   async.times(iterator, function(number, next){
     console.log('iterating'+ number);
     var newItem = Item({
       id: number,
       market_hash_name: req.body.market_hash_name,
       appid: '730',
       quantity: req.body.quantity,
       price: req.body.price,
       median_week:  req.body.median_week,
       median_month: req.body.median_month,
       average_median_week: req.body.average_median_week,
       trend_week: req.body.trend_week,
       trend_month: req.body.trend_month,
       total_sold_week: req.body.total_sold_week,
       total_sold_month: req.body.total_sold_month,
       updated_at: req.body.updated_at
  })

  // save andt
  //  call next (which is callback)
           newItem.save(next);
       }, function(timesErr, timesResult){

           // if something failed, return error
           // even if 1 item failed to save, it will return error
           if(timesErr) {
               return res.send(400, {message: 'Something went wrong'});
           }
           // send the list of saved items;
           // or whatever you want
           res.status(200).send(timesResult);
       });
     }
 });

});

此问题可参考this issue

2 个答案:

答案 0 :(得分:0)

试试此代码

var app = require(' express')(); var bodyParser = require(' body-parser');

app.use(bodyParser.json());

app.get .....

答案 1 :(得分:0)

这个问题很老了。对于任何有同样问题的人来说,这就是解决方案。您必须先将正文内容解析为json,然后才能访问param。像const content = JSON.parse(body)

相关问题