简体   繁体   中英

Firebase database rules. Unable to write data

I am new to firebase rules. And I am not able to write data to user node who is currently logged in. My db structure is,

userdata: {
  profile: {
    Lp6Sj763bVUXlisPJyaLM3gbns73: {
      name: "sample"
      avatar: "sample.png",
      userid: "albdchalwe"
    },
    FRw6J6GWK6YbvhuaaHNzPtLRSHJ2: {
      name: "sample"
      avatar: "sample.png",
      userid: "calisdnca"
    }
  },
  additionaldata: {
    Lp6Sj763bVUXlisPJyaLM3gbns73: {
      notifications: {
        0: {
          notificationId: "cbawln"
          receviedon: 12212
        },
        1: {
          notificationId: "cbawln"
          receviedon: 12212
        }
      }
    },
    FRw6J6GWK6YbvhuaaHNzPtLRSHJ2: {
      notifications: {
        0: {
          notificationId: "cbawln"
          receviedon: 12212
        },
        1: {
          notificationId: "cbawln"
          receviedon: 12212
        }
      }
    }
  }

And wanted to give write access to node which is related to currently logged in user. My database rules are,

{
  rules: {
    userdata: {
     profile: {
       .read: "auth != null",
       .indexOn: ["name"]
       $userId: {
         avatar: {
           .write: "$userId == auth.uid"
         },
         avatar: {
           .write: "$userId == auth.uid"
         }
         userid: {
           .write: "$userId == auth.uid && !data.exists()"
         }
       }
     }
   }
 }

user node under profile contains more nodes. I just added 3 sub nodes fro user node to understand the structure. Leave the additionaldata node for now.

My problem is unable write the data to profile node. When I run it on simulator, I am getting the following,

[ 在此处输入图片说明

在此处输入图片说明 在此处输入图片说明

Can anyone help me fixing this. Thanks in advance.

Is it because you are writing to /userdata/profile/$uid instead of /userdata/profile/$uid/avatar where the .write is defined? (It is also duplicated in your example)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM