0

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

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

项目是通过 express-generator 创建的(安装了 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);
       });
     }
 });

});

这个问题被提到这个问题

4

2 回答 2

0

试试这个代码

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

app.use(bodyParser.json());

应用程序.get.....

于 2016-06-05T20:00:25.157 回答
0

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

于 2016-07-13T22:01:25.643 回答