Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Add Elements To Firebase Realtime Database With Index As Keys

In my Firebase Realtime Database I have a structure like this:

Database Screenshot

Now I want to implement a function that adds habits to this collection. Right now I am using the following code (with input.value being the value of an input element). However, the Firebase push method creates a random key everytime it adds a new element. I would like the keys to be numbers / index values, just like the elements in the screenshot have the keys 0 and 1. Is there a Firebase method that works like this (if so, I couldn't find it), and if not, how would I best implement this, so that the next element would be added with the key 2 and so on?

function addHabit() {
  push(reference(database, "habits"), {
    text: input.value,
    interval: "Daily",
  });
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using sequential numeric keys with Firebase is an anti-pattern as determining the next index requires you to read the existing length (so won't work while offline) and that all users agree on that length (so won't work in multi-user scenarios). To store lists of data, it's better to use Firebase's push() operator.

For more on this, see Best Practices: Arrays in Firebase.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!