I am trying to fetch some data from firebase realtime database and insert the data into an array. This is my code:
function get_paydata(key,emp){
var results = [];
firebase.database().ref("pay/0/0/").once('value', function(snapshot){
snapshot.forEach(function (child){
if(Object.entries(child.val())[2][1] == emp && Object.entries(child.val())[10][1] == key){
console.log(child.val()); //shows the result after some time
var temp = []; ^
temp.push(Object.entries(child.val())[0][1]); |
temp.push(Object.entries(child.val())[1][1]); |
results.push(temp); |
} -----
}) |
}) |
return results; |
} |
|
(async() => { |
var results = await get_paydata("Electric bill",emp); |
if(results.length != 0) |
console.log(results); |
else |
console.log("empty array"); // this shows and then ----
})();
The empty array
line prints in the console and then child.val()
prints. Why can't I store the values into the array?