I have tried to save snapshot properties in arrays, but for some reason, when I store the data inside the query, for example with on
or child_added
and run the data with a foreach
, the data is displayed inside the Loop and the query, but out of this its value is undefined, I tried with objects but only rescued the last value, How could this problem be solved?
Here's a code snippet:
var array=[];
var ref= firebase.database().ref().child('messages');
ref.orderByChild("fecha").on('value',function(snapshot){
snapshot.forEach(function(snap){
//test array
array[0] = snap.val().text;
console.log(array[0]);//show the data
});
});
//but out of the loop
console.log(array[0]);//Return undefined