0

my issue is that req.body.param is undefined but body itself returns the wanted json from the requested source.

This means my iteration wont work too because i need to access req.body.data.length or the count of the id param available. Thats also why i set num of iterations to 5 to test the output.

Project is created via the express-generator(body-parser is installed).

This is what i get returned

[
 {
  __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"
 },  ... 
]  

This is how the requested api json body looks like:

{
 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,
          ...,
         }   
       }  

My Function

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 Problem is referred to this issue

Community
  • 1
  • 1
Simon D
  • 29
  • 1
  • 6
  • I think the problem is that we try to access those params through express request object which handles only local routes. We get the body when request.get is called. So we can easily log(body); and retrieve the whole json body. Whereas accessing its properties wont work and returns undefined. – Simon D Jun 05 '16 at 21:11
  • This question is old. For anyone having the same problem here is the Solution. You have to parse body content to json after that you´ll be able to access the param´s. like so const content = JSON.parse(body) – Simon D Jul 13 '16 at 22:00

2 Answers2

0

Try this code

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

app.use(bodyParser.json());

app.get.....

Alongkorn
  • 3,968
  • 1
  • 24
  • 41
0

This question is old. For anyone having the same problem here is the Solution. You have to parse body content to json after that you´ll be able to access the param´s. like so const content = JSON.parse(body)

Simon D
  • 29
  • 1
  • 6