【问题标题】:Firebase Rules Wildcard and Child comparisonFirebase 规则通配符和子级比较
【发布时间】:2019-01-27 07:53:51
【问题描述】:

我正在尝试将 Firebase 的规则通配符与子比较混合。

我在其他地方读到一个值为“4”的孩子。

当我进行字面比较时,模拟器给了我绿灯(像这样):

{
  "rules": {
    "die": {
      "rolls": {
        "$i": {
          ".read": "4 == root.child('die/i').val()"
        }
      },
      "i": {
        ".read": true,
        ".write": true
      }
    }
  }
}

输出(成功):

Type    read
Location    /die/rolls/4
Data    null
Auth    null
Read successful
Line 7 (/die/rolls/4)
read: "4 == root.child('die/i').val()"

但是通配符比较失败。为什么?

{
  "rules": {
    "die": {
      "rolls": {
        "$i": {
          ".read": "$i == root.child('die/i').val()"
        }
      },
      "i": {
        ".read": true,
        ".write": true
      }
    }
  }
}

输出(失败):

Type    read
Location    /die/rolls/4
Data    null
Auth    null
Read denied
Line 7 (/die/rolls/4)
read: "$i == root.child('die/i').val()"

(另外,我尝试过模拟身份验证;同样的事情。)

【问题讨论】:

  • 将其转换为文本并给出了我能给出的错误/日志。谢谢
  • 感谢! (我无法回答你的实际问题,但祝你好运)
  • Alex,如果@Padawan 的回答没问题,你真的应该选择它。

标签: firebase firebase-realtime-database real-time wildcard firebase-security


【解决方案1】:

失败的原因是因为

root.child('die/i').val()

返回一个数字。根据 Firebase 文档

注意:路径键始终是字符串。出于这个原因,重要的是要记住,当我们尝试将 $ 变量与数字进行比较时,这总是会失败。这可以通过将数字转换为字符串来纠正(例如 $key === newData.val()+'')

以下内容为您提供您想要的结果

 {
 "rules": {
   "die": {
     "rolls": {
       "$i": {
         ".read": "$i === root.child('die/i').val()+''"
       }
     },
     "i": {
       ".read": true,
       ".write": true
     }
   }
 }
}

Firebase documentation

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2021-12-12
    • 2016-10-12
    • 2014-06-09
    相关资源
    最近更新 更多