Firebase 规则通配符和 Child 比较
Firebase Rules Wildcard and Child comparison
我正在尝试将 Firebase 的规则通配符与 children 比较混合使用。
我正在阅读其他地方的 child,其值为“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()"
(另外,我试过模拟身份验证;同样的事情。)
失败的原因是
root.child('die/i').val()
returns一个数字。根据 firebase 文档
Note: Path keys are always strings. For this reason, it's important to keep in mind that when we attempt to compare a $ variable to a number, this will always fail. This can be corrected by converting the number to a string (e.g. $key === newData.val()+'')
以下是您想要的结果
{
"rules": {
"die": {
"rolls": {
"$i": {
".read": "$i === root.child('die/i').val()+''"
}
},
"i": {
".read": true,
".write": true
}
}
}
}
Firebase documentation
我正在尝试将 Firebase 的规则通配符与 children 比较混合使用。
我正在阅读其他地方的 child,其值为“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()"
(另外,我试过模拟身份验证;同样的事情。)
失败的原因是
root.child('die/i').val()
returns一个数字。根据 firebase 文档
Note: Path keys are always strings. For this reason, it's important to keep in mind that when we attempt to compare a $ variable to a number, this will always fail. This can be corrected by converting the number to a string (e.g. $key === newData.val()+'')
以下是您想要的结果
{
"rules": {
"die": {
"rolls": {
"$i": {
".read": "$i === root.child('die/i').val()+''"
}
},
"i": {
".read": true,
".write": true
}
}
}
}
Firebase documentation