Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda