如何通过 Firebase Admin 访问深度数据?
数据:
{
"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());
});
但我不知道如何获得SecendKeyboard
/childs
的childs
. 我的意思是Keyboard1
、Keyboard2
和Keyboard3
的数组. 谢谢。
要获取键盘子项:
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());
}
})
});