I have this function is javascript that I hope to construct an array from data I have stored in my firebase database. It is evident that the array is being created properly (by use of console.log statements), but I cannot get the array outside of the function. So far, this is what I have:
// my function
function foo(){
firebase.database().ref("data").once('value').then(function(snapshot){
var list = snapshot.val();
var newlist = [];
for(var item in list){
newlist.push(
{axis:"x-axis", value:list[item].info, definition: list[item].def},
{axis:"y-axis", value:list[item].info, definition: list[item].def},
{axis:"z-axis", value:list[item].info, definition: list[item].def},
);
}
return newlist;
})
}
// my creation
var fooList = foo();
// my test
setTimeout(function(){
console.log(fooList); // returns undefined...
},5000);
Whenever I run this function, I always get an undefined element back. I would really appreciate the help!