在获取Firebase数据库的引用时,是否可以使用通配符来构建路径? [英] Can I use wildcard to build paths when getting references of firebase database

查看:99
本文介绍了在获取Firebase数据库的引用时,是否可以使用通配符来构建路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Firebase中有以下数据库结构:

  {
level1:{
0 :{
prop1:{},
prop2:{}
},
1:{
prop1:{},
prop2:{}
},
2:{
prop1:{},
prop2:{}
}
}
}

如果我只想捕捉 prop1 level1 ,我可以使用通配符来构建一个引用路径: level1 / * / prop1
我不认为这是可能的,但我只是要求证实,因为我没有在文档中发现它提到它。



调用到 prop1 的列表,理论上看起来像这样: firebase.database()。ref('level1 / * / prop1')



是的,我试图避免将这些 props 拆分成不同的节点,然后不得不相互引用一个。懒惰的程序员在这里。

解决方案

我继续测试它。在构建用于捕获Firebase数据库引用的路径时,不可能使用通配符。取而代之的是只读取 prop1 ,数据库结构必须修改。新结构将如下所示:

  {
level1:{
prop1level:{
0:{prop1:{}},
1:{prop1:{}},
2:{prop1:{}}
},
prop2level:{
0:{prop2:{}},
1:{prop2:{}},
2:{prop2:{}}
}
}
}

使用这个新的数据库结构,我可以为不同的属性设置不同的规则。 p>

If I have the following database structure in firebase:

{
    level1 : {
        0: {
            prop1: {},
            prop2: {}
        },
        1: {
            prop1: {},
            prop2: {}
        },
        2: {
            prop1: {},
            prop2: {}
        }
    }
}

If I wanted to catch only the list of prop1 inside level1, could I build a reference path with wildcards like so: level1/*/prop1? I don't think this is possible, but I am asking just to confirm, since I didn't find mentions to it in the documentation.

The call to the list of prop1, theoretically, would look like this: firebase.database().ref('level1/*/prop1').

Yes, I am trying to avoid to split those props into different nodes and then having to reference one to the other. Lazy programmer here.

解决方案

I moved on to test it. It is not possible to use wildcards when building paths to be used to catch firebase database references. Instead, do fetch just prop1, the database structure has to be modified. The new structure will look like the following:

{
    level1 : {
        prop1level: {
            0: {prop1: {}},
            1: {prop1: {}},
            2: {prop1: {}}
        },
        prop2level: {
            0: {prop2: {}},
            1: {prop2: {}},
            2: {prop2: {}}
        }
    }
}

With this new database structure I am able to set different rules for the different properties.

这篇关于在获取Firebase数据库的引用时,是否可以使用通配符来构建路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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