Home > other >  Firebase: How to check if a value exists in the database and store that value in an HTML table?
Firebase: How to check if a value exists in the database and store that value in an HTML table?

Time:10-20

I want to store some particular values from my snapshot fetched, if those values exist. This is my code:

function paydata(){
    firebase.database().ref("pay/0/0/").once('value', function(snapshot){

        var resp = [];
        resp.push(snapshot.val());
        console.log(resp); //this prints the complete database

        for(var i=0; i<resp[0].length; i  ){
            if(resp[0][i]["Employee"] == "22729805418" && resp[0][i]["Type"] == "AC bill"){

                 console.log(resp[0][i]); //this never prints even if the condition is true
                  
                 //Again I want to store all the corresponding values in my HTML table
                 var row = table.insertRow(table.rows.length);
                 var cell = row.insertCell(0);
                 var cell1 = row.insertCell(0);
                 
                 cell.innerText = resp[0][i]["Actual amount"];
                 cell1.innerText = resp[0][i]["Current reading"];  
            }
        }
    })

    console.log(table); //doesn't print
}

paydata();

My complete database is like this:

enter image description here

How do I get those particular values that match the if condition and store them in the HTML table?

CodePudding user response:

Judging from your screenshot of the database, it looks like resp[0] is an object, not an array, so the for loop needs to be changed to for(var i in resp[0])

  • Related