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.
<script>
// 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 = '<ul class="mdl-list" id="emergenciesList">',
closetag = '</ul>',
array = [];
for (i = 1; i <= emergencies.length; i++) {
array[i] = '<li class="mdl-list__item">' + emergencies[i] + '</li>';
}
var newArray = array.join(" ");
document.getElementById('foo').innerHTML = opentag + newArray + closetag;
</script>
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!