Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 238790f

Browse files
committed
[docs] - Update firestore docs for onSnapshot
1 parent 63a00c1 commit 238790f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

docs/FIRESTORE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ const unsubscribe = citiesCollection.onSnapshot((snapshot: firestore.QuerySnapsh
6060

6161
// then after a while, to detach the listener:
6262
unsubscribe();
63+
64+
/**
65+
* If you pass in SnapshotOptions for the first parameter then your next should be onNext and
66+
* onError if you want for the third parameter. If you pass onNext as the first parameter the
67+
* second will be interpreted as onError callback. Note that you could pass onComplete, but
68+
* it will never be called as stated by Firestore docs.
69+
*
70+
* onError callbacks are optional!
71+
* onSnapshot(p1: SnapshotListenOptions|onNextCallback, p2?: onNextCallback | onErrorCallback, p3?: onErrorCallback?)
72+
*/
73+
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
74+
docRef.onSnapshot(
75+
{includeMetadataChanges: true}, // Comment out if you just want onNext && onError callbacks
76+
(doc: firestore.DocumentSnapshot) => {
77+
78+
const source = doc.metadata.fromCache ? "local cache" : "server";
79+
console.log("Data came from " + source);
80+
console.log("Has pending writes? " + doc.metadata.hasPendingWrites);
81+
},
82+
(error: Error) => {
83+
console.error(error);
84+
}
85+
);
6386
```
6487

6588
> Using **Observables**? [Check the example in the demo app](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/f6972433dea48bf1d342a6e4ef7f955dff341837/demo-ng/app/item/items.component.ts#L187-L198).

0 commit comments

Comments
 (0)