Bas H
Bas H

Reputation: 2226

Allow write to one child in Realtime Firebase database

In a Realtime Firebase database there are a lot off Childs

And in those Childs the are some more.

What rule's do i add so Read = true Butt only write to the child "Pupil" and everything in that child

   "rules": 
{
".read": true,
".write": false
}
"Pupil": 
 {
  ".read": true,
  ".write": true
 }
}

I am missing a Expected ',' or '}'.

Upvotes: 0

Views: 65

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600141

The security rules for the Firebase Realtime Database as in JSON format, and what you have is not valid JSON. In fact, it's not even close.

I highly recommend using a tool that can show you whether your code is valid JSON (a quick search can help you find one of these).

The closest valid JSON equivalent to what you have seems to be:

{
  "rules": {
    ".read": true,
    ".write": false
  },
  "Pupil": {
    ".read": true,
    ".write": true
  }
}

While this is now valid JSON, it is not a valid set of security rules, as you can only have one top-level event (names rules) in security rules, and the rest of your rules needs to be under that.


To make it into valid security rules, nest the Pupil node under the top-level rules:

{
  "rules": {
    ".read": true,
    ".write": false,
    "Pupil": {
      ".read": true,
      ".write": true
    }
  }
}

So you should be able to save the above in the Firebase console without an error.


You can simplify this a bit to:

{
  "rules": {
    ".read": true,
    "Pupil": {
      ".write": true
    }
  }
}

The top-level read permission is automatically inherited by all nodes under the root. And by default there is no permission, so the ".write": false on the root is implied here.

Upvotes: 1

Related Questions