Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading