Olli
Olli

Reputation: 87

Create new path in Firebase Realtime Database programmatically?

In Firebase documentation I don't found the anweser. How to add a new path in real-time database programmatically in Javascript? Like (just an example):

standard path: https://testapp.firebaseio.com/

And in the app I want to say something like: If this, then create a path like this: https://testapp.firebaseio.com/info/important/personal

Upvotes: 1

Views: 3011

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599836

Keys and paths in the Firebase Database are automatically created when you write a value to them. So to generate the path info/important/personal, you would write a value under it. For example:

firebase.database().ref('info/important/personal').set(true);

This will generate the following JSON:

{
  "info": {
    "important": {
      "personal": true
    }
  }
}

Note that the inverse is also true: Firebase will automatically remove empty keys/paths. So if you delete the true above, the entire JSON will be deleted.

Upvotes: 4

Related Questions