Javascript 无法检索firebase数据的密钥

Javascript 无法检索firebase数据的密钥,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我无法使用以下结构(部分)从firebase数据库检索密钥 下面是我用来检索数据的代码(来自示例)。我包括了警报,以查看代码是否进入循环,但它没有 firebase.initializeApp(config); var hello = document.getElementById("hello"); var dbref = firebase.database().ref().child("roomlist"); //dbref.on("value", snap =>

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

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

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


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

我知道了。显然,您不需要放置()。所以我改变了

var key = childSnapshot.key();


它成功了。

我明白了。显然,您不需要放置()。所以我改变了

var key = childSnapshot.key();


这是我们从2.x SDK到3.x版本所做的更改之一。很高兴听到您找到了它。这是我们从2.x SDK到3.x版本所做的更改之一。很高兴听到你找到了它。
var key = childSnapshot.key();
var key = childSnapshot.key;