2016-07-14 14 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バージョンに加えた変更の1つです。あなたがそれを見つけたと聞いてうれしいです。 –

関連する問題