Edgar
Edgar

Reputation: 6856

Add new item to Realtime Database array

How I could append an element to an array like that ?.

enter image description here

Upvotes: 0

Views: 800

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

Adding an item to an array structure like that, requires three steps:

  1. Read the existing data.
  2. Determine the key of the new item.
  3. Write the new item.

In code that'd be something like:

const ref = admin.database().ref("javascript");
ref.once("value").then((snapshot) => {
  let numChildren = parseInt(snapshot.numChildren());
  ref.child(""+(numChildren+1)).set(4);
});

Note that this type of data structure is fairly non-idiomatic when it comes to Firebase, and I recommend reading:

Upvotes: 1

Related Questions