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

161
Vistas
How to deal with null value in realtime database transaction?

I would like to have a counter that we can increment or decrement using transaction. I would like not to initialize the counter to 0, insteads it doesn't exist at start. So I would write the increment transaction as below

ref.transaction(function(counterValue){
    if (counterValue == null) return 1
    else return (counterValue +1)
},function(error,committed,snapshot){
   if (error){//fail
   }else if (!committed){//aborted
   }else{//successfull
   }
})

But I'm not sure if the transaction will run correctly since the null value of counterValue could mean no existing node but also when local cache is empty. Could anyone please advise me what I should do?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

the null value of counterValue could mean no existing node but also when local cache is empty

If the existing value is reported as null because the local cache doesn't contain a current value, the new value you return doesn't really matter as your handler will be rerun anyway.

So your handler looks fine to me, although I typically shorten it to:

return (counterValue || 0) + 1
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