Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
How to use ref.once with Firebase 9?

All of the docs that have code examples related to firebase.database are for version 8 and below. I've intuited a lot from exploring the exported modules themselves, but one thing I can't figure out how to update is code like ref.once('value').

Other code is now like:

ref.update(data); // old
update(ref, data); // new

But there is no once to do once to use like once(ref, data). Additionally the created refs seem to lack any property to use.

enter image description here

How is this meant to be updated for Firebase 9.x? The Firebase 9 docs don't seem to offer any example.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

firebaser here

The once('value' method has been replace by a get() function, so:

const snapshot = await get(ref);

Also see the Firebase documentation on reading data once.


To unsubscribe from a realtime listener (so not get()) you now can use the return value from the function you called to subscribe.

So if you subscribe with:

const unsubscribe = onValue(ref, (snapshot) => { ... });

You can then later unsubscribe with:

unsubscribe();

This is what the Firestore SDKs already did, and feedback indicated that devs preferred that over off.

about 4 years ago · Juan Pablo Isaza Relatório

0

Heads up (~24 Jun 2022; firebase v9): While get() is the recommended way to read data once (see Frank's post), there are currently some unresolved issues with get() where a client's execution of multiple queries with different filters on the same ref path somehow causes interference between the queries resulting in the unexpected triggering of events (see links below). I also experienced this and it's quite hard to debug. It is probably caused by get()'s caching mechanism as hinted at by the issues below:

  • [realtime database] concurrent queries on the same path don't work #515
  • Queries to the same database path get mixed up and return incorrect results depending on order of execution #6038

Workaround: Instead of retrieving results via get(), you can also use onValue() (see value observer) and then disconnect it immediately after the item has been returned:

const val = await new Promise((resolve, reject) => {
  onValue(query, (snap) => resolve(snap.val()), { onlyOnce: true });
});

Disclaimer: Note that onValue() in combination with onlyOnce: true will return values from firebase's local disk cache immediately (if present), instead of checking for an updated value on the server first (as it is the case with get()).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda