【问题标题】:Firebase Realtime Database - RulesFirebase 实时数据库 - 规则
【发布时间】:2020-11-08 01:04:09
【问题描述】:

我的 Firebase RTD 的结构如下:

messages :{
   userId1: {
               userId2: {sentFrom: userId1, messageText: 'hello user 2 from user 1'}
            },
   userId2: {
               userId1: {sentFrom: userId1, messageText: 'hello user 2 from user 1'}
            },
}

如果路径是message/userId1,我希望 user1 能够写入 DB .....但是如果路径是 message/anyOtherUserId/userId1,我也希望 user1 能够写入

我在规则中使用的 json 如下......当我点击路径 message/userId1 时它成功......但是当我点击 message/anyOtherUserId/userId1 它返回“模拟集被拒绝”

{
  "rules": {
    "messages": {
        "$userId": {
          ".write": "$userId === auth.uid ? true : (root.child($userId).val()===auth.uid ? true : false)",
        }, 
    }
  } 
}

【问题讨论】:

  • 请编辑您的问题以在收到错误时显示模拟器设置。确保它显示您编写的路径和值、您使用的设置以及失败的规则。另外:显示实际的 JSON,而不是“伪代码”。您可以通过单击Firebase Database console 上溢出菜单 (⠇) 中的“导出 JSON”链接来获取此信息。

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


【解决方案1】:

您可能需要进一步级联规则。在你的模拟中试试这个:

{
    "rules": {
        "messages":{
          "$userId":{
            ".write": "$userId === auth.uid",
            "$userIdChild": {
              ".write": "$userIdChild === auth.uid"
            }
          }
        }
    }
}

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2021-01-31
    • 2020-08-08
    • 2020-02-16
    • 2019-10-11
    • 2019-01-18
    相关资源
    最近更新 更多