无法检索firebase数据的密钥

时间:2016-07-14 12:21:16

标签: javascript firebase firebase-realtime-database

我无法使用以下结构(部分)从我的firebase数据库中检索密钥。

{
"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"
}}

以下是我用来检索数据的代码(来自示例)。我包括警报以查看代码是否进入循环,但它没有。

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;
        });
    });

并且

hello.innerText = snapshot.numChildren();

返回20.这是数据库中的实际数据数。

1 个答案:

答案 0 :(得分:4)

我明白了。显然你不需要把()。所以我改变了

var key = childSnapshot.key();

var key = childSnapshot.key;

并且有效。

相关问题