javascript - 通过 Firebase Admin 访问深层数据

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

但我不知道如何获取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());
        }
    })
});

关于javascript - 通过 Firebase Admin 访问深层数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45570523/

相关文章:

javascript - 使用 CKeditor 工具获取电子邮件上的 HTML 标签后

javascript - 无法读取 null 的属性 'offsetHeight'

javascript - 我可以在 app.get 中使用不同路径的 app.use

javascript - 放大和缩小 Threejs

firebase - 将 Firebase 集成到 Vue.js 2 时出现“未捕获的 TypeError : firebaseApp. firestore 不是函数”

java - 从 Firestore 增加值(value)

android - 图片网址不会被推送到 Firebase 数据库

firebase - 如何正确查询 firestore 和 cloudfunctions 中的日期时间段

swift - firebase firebase 快照作为自定义模型

java - 赋予 firebase 数据库名称等于 USER ID 键的子级