如何在Firebase中动态生成的引用中找到引用? [英] How can I get to the references inside dynamically generated references in firebase?

查看:63
本文介绍了如何在Firebase中动态生成的引用中找到引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在firebase中,我试图创建和访问子节点的数据库ref,该子节点的路径中间具有推送" ID. 我想命名为:烹饪区",位于部门"内的部门(dept1)内.问题在于,"dept"位于由Firebase(推送ID)随机生成的数据库引用中.
我的问题是:如何获得L3s_dBCdKN4EyRJAi中的引用?

In firebase I am trying to create and access a database ref of a child node that has a "push" ID in the middle of the path. I want to get to the name: "Cooking area" which is inside a department (dept1) which is inside "depts". The problem is that "depts" is inside a database reference that is randomly generated by firebase (the push ID).
My question is: how can I get to the references inside that L3s_dBCdKN4EyRJAi?

export const firebaseApp = Firebase.initializeApp(config);
export const db = firebaseApp.database();

export const spacesRef = db.ref('spaces'); // this WORKS

export const deptsRef = spacesRef.child('depts'); // this DOESN'T!!!

推荐答案

最简单的答案是,你不能,而不是不查询所有空格".当您遇到这样的问题时,您应该重新考虑数据结构. Firebase鼓励您避免嵌套数据.因此,与其在您的空间下没有一个depts树,不如在您的根目录下有一个depts树(其子代与您的空间具有相同的ID),以便您可以更轻松地将其用作参考.

The short answer is you can't, not without querying all your 'spaces'. Chances are when you hit a problem like this you should rethink you data structure. Firebase encourages you to Avoid Nested Data. So rather than having a depts tree under your space you could have a depts tree at your root (with children of the same id as your space) so you can more easily use it as a reference.

{
    "spaces": {
        "L3s__dBCdKN4EyRJfAi": {
            "address": "...",
            "comments": "...",
            "name": "..."
        }
    },
    depts: {
        "L3s__dBCdKN4EyRJfAi": {
            "dept1": {
                "name": "...",
                "numberOfRooms": "..."
            }
        }
    }
}

这篇关于如何在Firebase中动态生成的引用中找到引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆