voluptasmollitia
Version:
Monorepo for the Firebase JavaScript SDK
33 lines (19 loc) • 2 kB
Markdown
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[](./index.md) > [@firebase/database](./database.md) > [onChildAdded](./database.onchildadded_1.md)
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](https://firebase.google.com/docs/database/web/retrieve-data) for more details.
An `onChildAdded` event will be triggered once for each initial child at this location, and it will be triggered again every time a new child is added. The `DataSnapshot` passed into the callback will reflect the data for the relevant child. For ordering purposes, it is passed a second argument which is a string containing the key of the previous sibling child by sort order, or `null` if it is the first child.
<b>Signature:</b>
```typescript
export declare function onChildAdded(query: Query, callback: (snapshot: DataSnapshot, previousChildName: string | null) => unknown, options: ListenOptions): Unsubscribe;
```
| Parameter | Type | Description |
| --- | --- | --- |
| query | [Query](./database.query.md) | The query to run. |
| callback | (snapshot: [DataSnapshot](./database.datasnapshot.md)<!-- -->, previousChildName: string \| null) => unknown | A callback that fires when the specified event occurs. The callback will be passed a DataSnapshot and a string containing the key of the previous child, by sort order, or <code>null</code> if it is the first child. |
| options | [ListenOptions](./database.listenoptions.md) | An object that can be used to configure <code>onlyOnce</code>, which then removes the listener after its first invocation. |
<b>Returns:</b>
[](./database.unsubscribe.md)
A function that can be invoked to remove the listener.