如果存在,如何禁止重写数据



我在授权后将用户添加在表"用户"中。另外,我还将向该用户添加一些值,我不想被覆盖。

ref.child("users").child((FIRAuth.auth()?.currentUser?.uid)!).setValue(["username": "MyName"])

规则

{
    "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
    "users": {
      ".write": "!root.child('users/'+auth.uid).exists()",
      ".read":true,
      "positive": {
        ".read": true,
          ".write":false,
      },
      "negative": {
        ".read": true,
          ".write":false,
      },
    }
  }
}

删除旧数据并放置一个新数据。

我想在服务器端编写规则,如果已经存在,它将忽略设置值。

如果您致电updateChildValues(),则指定的属性将被更新:

ref.child("users")
   .child((FIRAuth.auth()?.currentUser?.uid)!)
   .updateChildValues(["username": "MyName"])

请参阅有关更新特定字段的Firebase文档。

您的规则将不起作用,因为您无法在树上较高层次上获得允许的许可。请参阅有关权限级联的Firebase文档。

确保用户始终具有某些属性,请在用户节点上使用规则:

"users": {
  "$uid": {
      ".write": "newData.hasChildren(['certain', 'properties])",

最新更新