【问题标题】:Can I create a rule in firebase that will query a value in a push() array?我可以在 firebase 中创建一个规则来查询 push() 数组中的值吗?
【发布时间】: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


【解决方案1】:

您似乎正在尝试“过滤”组数据,这不是 Firebase 规则的用途。参考链接:
https://firebase.google.com/docs/database/security/securing-data#rules_are_not_filters

对于您想要实现的目标(限制对组的读取访问),您需要将数据模型调整为您的应用需要访问它的方式。让我知道这是否是您正在寻找的内容,我可以更新我的答案。

【讨论】:

  • 是的,我想申请一个查询。重组成功了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 2011-06-16
  • 2017-01-09
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多