F0r3v3r-A-N00b
F0r3v3r-A-N00b

Reputation: 3003

Unable to retrieve keys of firebase data

I'm not able to retrieve keys from my firebase database with the following structure (partial).

{
"Belmonte" : 
{
    "p1" : "p1",
    "p2" : "p2",
    "p3" : "p3",
    "p4" : "p4"
},
"Bricktown" : 
{
    "p1" : "p1",
    "p2" : "p2",
    "p3" : "p3",
    "p4" : "p4"
},
"Divino Rostro" : 
{
    "p1" : "p1",
    "p2" : "p2",
    "p3" : "p3",
    "p4" : "p4"
},
"Esperanza" : 
{
    "p1" : "p1",
    "p2" : "p2",
    "p3" : "p3",
    "p4" : "p4"
}}

And here is the code (from sample) that I use to retrieve the data. I included alert to see if the code enter the loop, but it doesn't.

firebase.initializeApp(config);
    var hello = document.getElementById("hello");
    var dbref = firebase.database().ref().child("roomlist");
    //dbref.on("value", snap => hello.innerText = snap.val());

    dbref.once("value", function(snapshot) {
        // The callback function will get called twice, once for "fred" and once for "barney"
        hello.innerText = snapshot.numChildren();
        var ctr = 0;
        snapshot.forEach(function(childSnapshot) {
            // key will be "fred" the first time and "barney" the second time

            var key = childSnapshot.key();
            var val = childSnapshot.val();

            ctr++;

            alert(ctr);

            // childData will be the actual contents of the child
            hello.innerText = ctr;
        });
    });

And

hello.innerText = snapshot.numChildren();

returns 20. This is the actual number of data in the database.

Upvotes: 1

Views: 1078

Answers (1)

F0r3v3r-A-N00b
F0r3v3r-A-N00b

Reputation: 3003

I got it. Apparently you do not need to put the (). So I changed

var key = childSnapshot.key();

to

var key = childSnapshot.key;

and it worked.

Upvotes: 4

Related Questions