【发布时间】:2016-06-05 14:21:55
【问题描述】:
我只希望用户能够加载组,如果他们的列表中有组,我将如何为此编写规则?
或者有没有办法编写规则来在 firebase 推送数组中查找值?
例如,我想编写一个规则,使其看起来像这样。此规则无效,但旨在解释我的观点。
"groups":{
"$group_id":{
".read":"root.child('users').child(auth.uid).child('groups').hasChildValue().val() == $group_id"
}
},
我只希望用户能够加载组,如果他们的列表中有组,我将如何为此编写规则?
更新,我是如何修复它的。 - 将数据重组为扁平化。摆脱使用 push() 来添加值。 - 平面数据使引用密钥变得容易。
"groups":{
// root/users/auth.uid/groups/$group_id
"$group_id":{
// only read if the user has the group_id
".read":"root.child('users').child(auth.uid).child('groups').child($group_id).exists()",
// only write if logged in and it's new || if the user has group id
".write":"(auth != null && !data.exists() && newData.exists()) || root.child('users').child(auth.uid).child('groups').child($group_id).exists()"
}
},
【问题讨论】:
-
".read":"root.child('users').child(auth.uid).child('groups').hasChild($group_id)", - 如果 group_id 有效是 push auto_id,但没有得到我想要的。
-
也许 .forEach 可以工作。如果您在函数中返回 false,我不确定 forEach 的返回值是什么。否则你可能想改变你的模型。我认为您的数据如下所示:firebase.com/docs/web/guide/…
-
没有 .forEach 可用于规则,除非我遗漏了什么。我正在考虑添加另一个关系,这样我就可以使它成为双向的,这样我就可以引用它而无需钻取 push 数组。
-
是的,你是对的,我把它和 DataSnapshot 混在一起了。我认为你唯一的选择是改变你的数据模型..
-
是的,你是对的,我不得不改变结构。现在就像一个魅力。
标签: firebase-realtime-database