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

159
Views
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 answers
Answer question

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 Report

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 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!