Home > @firebase/database > onValue
onValue() function
Listens for data changes at a particular location.
This is the primary way to read data from a Database. Your callback will be triggered for the initial data and again whenever the data changes. Invoke the returned unsubscribe callback to stop receiving updates. See Retrieve Data on the Web for more details.
An onValue
event will trigger once with the initial data stored at this location, and then trigger again each time the data changes. The DataSnapshot
passed to the callback will be for the location at which on()
was called. It won't trigger until the entire contents has been synchronized. If the location has no data, it will be triggered with an empty DataSnapshot
(val()
will return null
).
Signature:
export declare function onValue(query: Query, callback: (snapshot: DataSnapshot) => unknown, cancelCallback?: (error: Error) => unknown): Unsubscribe;
Parameters
Parameter | Type | Description |
---|---|---|
query | Query | The query to run. |
callback | (snapshot: DataSnapshot) => unknown | A callback that fires when the specified event occurs. The callback will be passed a DataSnapshot. |
cancelCallback | (error: Error) => unknown | An optional callback that will be notified if your event subscription is ever canceled because your client does not have permission to read this data (or it had permission but has now lost it). This callback will be passed an Error object indicating why the failure occurred. |
Returns:
A function that can be invoked to remove the listener.