0

I am trying to loop this entire 'request.post' but it comes up as an error: "TypeError: Cannot read property 'id' of undefined". Is there a way that I can loop the entire 'request.post'?

for (var i = 0; i < 2; i++){
request.post(
     'url',
     { json: {"category":category,"sub_category":subcategory,"contest_status":2} },
     function (error, response, body) {
         if (!error && response.statusCode == 200) {            
            console.log(body['contest_report'][i].id);
         }
     }            
); 
}     
coderrrr
  • 1
  • 3
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Jun 06 '18 at 15:05
  • `i` in the `post` callback is not what you think it is; see the linked question's answers for details. (Although it seems odd to be indexing into `body['contest_report']` using the loop counter for your request, and it seems odd to be making the same request repeatedly without any difference in parameters/post data...) – T.J. Crowder Jun 06 '18 at 15:07
  • Should the line starting "{ json: "be wrapped as a string constant, something like. '{ json: {\"category\":c.....s\":2}' . (Sorry, no idea how to escape double quotes in a string constant in javascript. It may even not need escaping, I forget.) – William J Bagshaw Jun 06 '18 at 15:12

0 Answers0