如何通过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());
});
但我不知道如何获得childs
/ SecendKeyboard
的{{1}}。
我的意思是childs
和Keyboard1
以及Keyboard2
的数组。
谢谢。
答案 0 :(得分: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());
}
})
});