title: Unable to retrieve firebase snapshot data into Javascript array
body: <p>I am trying to retrieve data from my database. My code creates an empty array and appends a child to it each time one is added. It then creates the list (ignore the MDL classes) and adds it to the HTML document.</p>
  
  <pre><code>&lt;script&gt;
      // get emergencies to array
      var firebaseRef = firebase.database().ref('Incidents');
      var emergencies = [];
      // var emergencies = ['Test', 'Test 2', 'Test 3'];
  
      firebaseRef.on('child_added', function(snap) {
          snap.forEach(function (childSnap) {
              console.log(childSnap.val());
              emergencies.push(childSnap.val());
          });
      });
  
      var opentag = '&lt;ul class="mdl-list" id="emergenciesList"&gt;',
      closetag = '&lt;/ul&gt;',
      array = [];
  
      for (i = 1; i &lt;= emergencies.length; i++) {
          array[i] = '&lt;li class="mdl-list__item"&gt;' + emergencies[i] + '&lt;/li&gt;';
      }
  
      var newArray = array.join(" ");
  
      document.getElementById('foo').innerHTML = opentag + newArray + closetag;
  &lt;/script&gt;
  </code></pre>
  
  <p>The weird thing is, that at the console.log() statement, the data is retrieved perfectly fine, but after the string manipulation, newArray is undefined. Help!</p>