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.
How is this meant to be updated for Firebase 9.x? The Firebase 9 docs don't seem to offer any example.
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.
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:
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()).