2017-08-08 117 views
0

如何通過Firebase管理訪問深層數據?通過Firebase管理訪問深層數據管理

數據:

{ 
    "keyboards": { 
     "StartKeyboard": [ 
      "KeyboardA", 
      "KeyboardB", 
      "KeyboardC" 
     ], 
     "SecendKeyboard": { 
      "parent": "StartKeyboard", 
      "childs": [  //*** I need to get this childs: [] *** 
       "Keyboard1", 
       "Keyboard2", 
       "Keyboard3" 
      ] 
     } 
    } 
} 

當我使用下面的代碼,我在輸出的所有數據

const ref = db.ref('/'); All Data 
ref.on("value", function (snapshot) { 
    console.log(snapshot.val()); 
    }); 

當我使用下面的代碼,我在輸出的keyboards孩子的

const ref = db.ref('keyboards'); // inside of Keyboards 
    ref.on("value", function (snapshot) { 
     console.log(snapshot.val()); 
     }); 

但我不知道如何獲得childsSecendKeyboard/childs。 我的意思是Keyboard1Keyboard2Keyboard3的陣列。 謝謝。

回答

1

爲了讓孩子鍵盤:

const ref = db.ref('keyboards/SecendKeyboard/childs'); 
ref.on("value", function (snapshot) { 
    console.log(snapshot.val()); 
}); 

或者:

const ref = db.ref('keyboards/SecendKeyboard'); 
ref.on("value", function (snapshot) { 
    console.log(snapshot.child("childs").val()); 
}); 

或者

const ref = db.ref('keyboards'); 
ref.on("value", function (snapshot) { 
    snapshot.forEach(function(childSnapshot) { 
     console.log(snapshot.val()); // prints StartKeyboard and SecendKeyboard 
     if (snapshot.child("SecendKeyboard").exists()) { 
      console.log(snapshot.child("SecendKeyboard").val()); 
     } 
    }) 
});