I have a security rule for adding new data

 "CATEGORIES": {
      "$CATEGORIES": {
      ".write":   "root.child('USERS/' + auth.uid + '/type').val() == 'admin' && !data.exists() || root.child('USERS/' + auth.uid + '/type').val() == 'admin' && root.child('MODES/delete').val() == 'yes'",
       "$DATA": {
         ".write":   "root.child('USERS/' + auth.uid + '/type').val() == 'admin'",
       }
      }
      }

It's used to prevent duplicates or overwriting of the child node with the !data.exists() rule.

Problems is, it wont let me delete the child with this rule because data exists. To get around this I added the root.child('MODES/delete').val() data to allow me to delete it if I set it to 'yes'.

How can I have it so I don't have to do this work around? So I can have no duplicates but also be able to delete the child if I want to?

有帮助吗?

解决方案

To allow creates but no overwriting, !data.exists() works fine. To allow deletes, you can use !newData.exists().

So all together:

// create or delete, but no update/overwrite
".write":  "!data.exists() || !newData.exists()"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top