I tried to get the data from Firebase and store it into an array. However, when I try to get the data from the array, it became undefined. Can anyone tell me why would it happened?
var events = [];
var databaseRef = database.ref("events").orderByChild("date");
databaseRef.on('child_added', function(snapshot) {
var event = snapshot.val();
events.push({
title: event.title,
content: event.content
});
});
console.log(events);
console.log(events[0]);
console.log(events.pop());
In #1, all the data is stored as object in the array. But in #2&3, it returns undefined
add: #1 returns:
[]
0 : Object
content : "Rehoncus. Aliquam nibh antegestas id dictum a, commodo. Praesenterto faucibus malesuada faucibu"
title : "Tivamus at magna non nunc"
1 : Object
content : "Rehoncus. Aliquam nibh antegestas id dictum a, commodo. Praesenterto faucibus malesuada faucibu"
title : "Vivamus at magna non nunc"