2016-07-14 165 views
1

我无法使用以下结构(部分)从我的Firebase数据库中检索密钥。无法检索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.这是数据库中的数据的实际数量。

回答

3

我明白了。显然你不需要把()。因此,我改变

var key = childSnapshot.key(); 

var key = childSnapshot.key; 

和它的工作。

+0

这是我们从2.x SDK向3.x版本所做的更改之一。很高兴听到您发现它。 –

相关问题