Firebase规则设置对特定属性的读/写

时间:2018-05-23 00:11:12

标签: firebase firebase-realtime-database firebase-security-rules

我正在火堆中存放无家可归者/紧急避难所的数据集。应该读取所​​有数据:true,并写入:false,除了我想写的OCCUPANCY属性:true。我一直在尝试基于其他堆栈溢出帖子(包括:this post using $other)的一些事情但是无法使其工作。我希望以下代码能够正常运行,我们将非常感谢任何建议。

{
 "rules": {
     ".read": true,
        "locations": {
            "$location": {
                OCCUPANCY": {
                    ".read": true,
                    ".write": true
                    },
                $other": {
                    ".read": true,
                    ".write": false
                    }
                }
       }
    }
}

enter image description here

我已在firebase模拟器中运行它,它似乎按要求工作,但在网站上它控制台Uncaught (in promise) Error: PERMISSION_DENIED: Permission denied

enter image description here

问题:向firebase发送更新的代码也在更新另外两个参数。如果部分更新没有写入所有权限的权限失败。

解决方案:更新firebase规则以允许写入这些参数,从嵌套参数中删除冗余读取语句(如下所示)。

  {
   "rules": {
       ".read": true,
       "locations": {
            "$location": {
                "OCCUPANCY": {
                    ".write": true
                    },
                "time": {
                    ".write": true
                    },
                "updated": {
                    ".write": true
                },     
                    "$other": {
                    ".write": false
                    }
                }
           }
        }
    }

0 个答案:

没有答案