title: Node JS ignores undefined check
body: <p>Im working on a NodeJs app that takes an event from FB and puts it into a local database. For every event in the first page of the api query this goes well, except for the last one.</p>
  
  <p>I am getting the following error:</p>
  
  <blockquote>
    <p>[December 1st 2016, 1:48:39 pm] TypeError: Cannot read property 'name'
    of undefined
        at IncomingMessage. (/home/node/virgo/app.js:217:32)
        at IncomingMessage.emit (events.js:129:20)
        at _stream_readable.js:907:16
        at process._tickCallback (node.js:372:11)</p>
  </blockquote>
  
  <p>Just after</p>
  
  <pre><code>console.log(resultBody);
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>function addFBEvent(facebookId){
  console.log("getting event: " + facebookId);
  
  var options = {
      hostname: 'graph.facebook.com',
      port: 443,
      path: '/v2.8/'+facebookId+'?fields=name,description,start_time,end_time,place,photos{images}&amp;access_token={INSERT API ACCESS CODE HERE}',
      method: 'GET'
  };
  
  https.request(options, function(res2) {
      var resultBody = "";
      res2.setEncoding('utf8');
      res2.on('data', function (chunk) {
          resultBody = resultBody + chunk;
      });
  
      res2.on('end', function () {
          dbConnection = sql.createConnection({
                                  host     : settings.dbHost,
                                  user     : settings.dbUser,
                                  password : settings.dbPassword
                              });
  
          dbConnection.connect(function(err){
                  if(!err) {
                      console.log("Database is connected ... nn");
                  } else {
                      console.log("Error connecting database ... nn");
                  }
          });
  
          var json = JSON.parse(resultBody);
  
          console.log(resultBody);
  
          if (json != undefined){
              var eventName = json.name;
              var eventStart = json.start_time;
              var eventEnd = json.end_time;
              var eventDescription = json.description;
              var eventPlace = json.place.name;
              var eventPoster = json.photos.data[json.photos.data.length-1].images[0].source;
              var eventId = json.id;
  
              console.log("name: " + eventName + ", start: " + eventStart + ", end: " + eventEnd + ", place: " + eventPlace + ", Poster: " + eventPoster);
              //console.log("Description: " + eventDescription);
  
              dbConnection.query('INSERT INTO SVVirgo.activities(title, description, image, start, end, price, location, facebook) VALUES ("'+eventName+'","'+eventDescription+'","'+eventPoster+'","'+eventStart+'","'+eventEnd+'",0,"'+eventPlace+'","'+eventId+'")', function (err, result){
  
              });
          }
  
          dbConnection.end();
      })
  }).end();
  }
  </code></pre>
  
  <p>See graph api explorer for the breaking event: <a href="https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&amp;path=1682486658666506%3Ffields%3Dname%2Cdescription%2Cstart_time%2Cend_time%2Cplace%2Cphotos%7Bimages%7D&amp;version=v2.8" rel="nofollow noreferrer">Graph API</a></p>
  
  <p>Event code: 1682486658666506</p>
  
  <p>I've tried catching the undefined json object with no luck, i've also tried to validate the json using this solution: <a href="https://stackoverflow.com/questions/3710204/how-to-check-if-a-string-is-a-valid-json-string-in-javascript-without-using-try/3710226">Stackoverflow validate JSON javascript</a></p>