通过Firebase管理员访问深度数据

时间:2017-08-08 14:04:19

标签: javascript firebase firebase-realtime-database firebase-admin

如何通过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}}。 我的意思是childsKeyboard1以及Keyboard2的数组。 谢谢。

1 个答案:

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