node JSON https request becoming undefined
<p>I am trying to send a single https request to an API, with a bunch of data in JSON format.</p>

<p>However, when running the code, I first ran into the problem that everything was undefined. I commented out my loop, trying to parse the data I needed, and right now I'm just trying to console.log all of the data.</p>

<p>However, it seems to still be looping through stuff somehow despite not having a loop anywhere in my code anymore.</p>

<p>here is the code for my request:</p>

<pre><code>function getCards() {
  // make a request
  var options = {
    host: 'omgvamp-hearthstone-v1.p.mashape.com',
    path: '/cards',
    method: 'GET',
  };

  var req = https.request(options, function(res) {
    console.log('STATUS ' + res.statusCode);
    console.log('HEADERS ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function(data) {
      //console.log(data);
      updateCardsCollection(JSON.stringify(data));
    });
  });

  req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
  });

  req.end();
}
</code></pre>

<p>The weird thing is that the console.log(data) in the above code, logs out all of the data just fine.</p>

<pre><code>function updateCardsCollection(data) {
  var cardsRaw = [];
  console.log("DATA");
  console.log("===========================================================");
  console.log(data.Classic);
}
</code></pre>

<p>Here "Classic" is one of the arrays of objects in the API.</p>

<p>Before implementing node, express, mongoose and jade. The following loop worked fine to parse through the data:</p>

<pre><code>   for(var key in data) {
     for(var i = 0; i &lt; data[key].length; i++) {
       console.log(data[key][i].cardId);
</code></pre>

<p>However the above beginning of a loop would print out an undefined error as well.</p>

<p>Another strange problem I seem to be having with this code is when I run it with the loop commented out, with just the 3 console.logs in the 'updateCardsCollection' function. It logs those 3 lines a lot of times. Despite the function only being called once.</p>

<p>Any idea why this code is no longer working for getting my API data?</p>