0

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Have you tried using `console.log()` inside your function `get_paydata()` ? Possibly you never reach inside the `forEach` or the if-condition – dsudo Oct 18 '22 at 07:21
  • Hello, I have updated my post. Please have a look –  Oct 18 '22 at 09:42

0 Answers0