如何通过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 of SecendKeyboard/childs。我指的是Keyboard1、Keyboard2和Keyboard3的数组。谢谢。
发布于 2017-08-08 14:39:35
要获得键盘子级:
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());
}
})
});https://stackoverflow.com/questions/45570523
复制相似问题