2016-07-14 176 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版本所做的更改之一。很高興聽到您發現它。 –