通过 Firebase Admin 访问深度数据



如何通过 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/childschilds. 我的意思是Keyboard1Keyboard2Keyboard3的数组. 谢谢。

要获取键盘子项:

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());
}
})
});

最新更新