首页 > 解决方案 > 只有用户可以修改在 Firebase 中有自己的数据

问题描述

我想让只有用户能够创建新帖子,并且只有用户才能修改他们创建的帖子。喜欢删除或编辑!

数据库结构

{
  "post" : {
    "-LWzHsKzAyK9Wfa1kbD_" : {
      "name" : "test",
      "title" : "test",
      "userId" : "8D3sENaBcLaXoGNnh1MPuoyj5LP2"
    },
    "-LWzHx6u-gQ7XoVR714a" : {
      "name" : "check",
      "title" : "check",
      "userId" : "WUM2HBkGo8TFDeOjEqO1s3lCj1p1"
    }
  }
}

我使用了这个规则,但我在保存时出错 No such method/property 'userId'.

{
    "rules": {
    ".read": false,
    ".write": false,
        "report": {
        ".read": "auth != null",
        ".write": "auth != null && ( (data.exists() && data.userId === auth.uid) || !data.exists() || !newData.exists() )"
        }
    }
}

标签: firebasefirebase-realtime-databasefirebase-security

解决方案


您不能只使用属性访问器表示法来获取数据的子值。您必须调用该child()函数来获取孩子,并调用该函数val()来获取其值。

所以:

data.child('userId').val() === auth.uid

推荐阅读