quodolores
Version:
Monorepo for the Firebase JavaScript SDK
33 lines (20 loc) • 1.82 kB
Markdown
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) > [@firebase/firestore](./firestore.md) > [/](./firestore_.md) > [onSnapshot](./firestore_.onsnapshot_3.md)
## onSnapshot() function
Attaches a listener for `DocumentSnapshot` events. You may either pass individual `onNext` and `onError` callbacks or pass a single observer object with `next` and `error` callbacks.
NOTE: Although an `onCompletion` callback can be provided, it will never be called because the snapshot stream is never-ending.
<b>Signature:</b>
```typescript
export declare function onSnapshot<T>(reference: DocumentReference<T>, options: SnapshotListenOptions, onNext: (snapshot: DocumentSnapshot<T>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void): Unsubscribe;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| reference | [DocumentReference](./firestore_.documentreference.md)<!-- --><T> | A reference to the document to listen to. |
| options | [SnapshotListenOptions](./firestore_.snapshotlistenoptions.md) | Options controlling the listen behavior. |
| onNext | (snapshot: [DocumentSnapshot](./firestore_.documentsnapshot.md)<!-- --><T>) => void | A callback to be called every time a new <code>DocumentSnapshot</code> is available. |
| onError | (error: [FirestoreError](./firestore_.firestoreerror.md)<!-- -->) => void | A callback to be called if the listen fails or is cancelled. No further callbacks will occur. |
| onCompletion | () => void | Can be provided, but will not be called since streams are never ending. |
<b>Returns:</b>
[Unsubscribe](./firestore_.unsubscribe.md)
An unsubscribe function that can be called to cancel the snapshot listener.