在Firebase中设置最大子项数

时间:2017-04-13 19:42:18

标签: ios swift firebase firebase-realtime-database firebase-security

还有一些其他(较旧)的问题,但由于它们已经有几年了,我很好奇是否有关于此的更新。我想为彩票制作动画,用户可以在其中购买门票。一切进展顺利,但我想确保售票不会超过一千张。它必须是服务器端,这些是我的规则:

  "Lottery": {
    ".read": "auth != null",
    ".write": "auth != null",
      "counter": {
        ".validate": "newData.isNumber() && newData.val() > 0 && newData.val() <= 1000 && ((!data.exists() && newData.val() === 1) || (newData.val() === data.val()+1))"
      },
        "tickets":{
          "root.child('Lottery/counter/').val() === someValueYouNeedItToEqual"
        }
  }

我不知道在someValueYouNeedItToEqual写什么。我担心这个系统的工作。我的目标是将用户的UID写入服务器,如果值(我可以搜索客户端的可用位置,值可以是0到1000之间的Int)是免费的,则会被接受。当拍摄所有斑点时(一个节点中有1000个孩子),应该拒绝它。我希望有人可以帮我找出所需的验证规则。谢谢。

2 个答案:

答案 0 :(得分:3)

这里有关于SO的指导:Limit number of records that can be written to a path (reference other paths in security rules)

或者您可以使用Cloud Functions for Firebase来实现database trigger两者:

  1. 随着孩子的来去,增加/减少子计数(在交易中是安全的)。
  2. 检查子计数以确保新子项对添加有效,并删除它(或其他一些孩子),如果没有。

答案 1 :(得分:2)

您收到错误是因为

"root.child('Lottery/counter/').val()"

表达式不返回布尔值。它返回存储在该引用处的任何值。您可以通过尝试

之类的方式来压缩错误
"root.child('Lottery/counter/').val() === someValueYouNeedItToEqual"