Search code examples
typescriptfirebasegoogle-cloud-firestorerxjsangularfire2

Is there a .get() in angular firestore?


I have seen there is a .valueChanges() and .snapshotChanges() which both listen to changes in the database but what if I want to retrieve the data just once? I noticed that adding .pipe(take(1)) at the end of .valueChanges() or .snapshotChanges() works but is it the same as .get() function that firebase firestore provides in admin sdk and mobile sdk for Android and iOS?

What I'm doing

 this.items = this.itemCollection.valueChanges().pipe(take(1));

What I'm expecting

 this.items = this.itemCollection.get();

PS: I'm new to react.


Solution

  • The Non-React way

    If you are looking for a way to do it without using react, you can call the get() method on the ref instead of the collection item.

    this.itemCollection.ref.get().then((querySnap) => {
         //use your data here
         console.log(querySnap.docs);
    });
    

    The React way

    this.itemCollection.get() returns an observable of type QuerySnapshot that you can subscribe to

    this.itemCollection.get().subscribe((querySnap) => {
     console.log(querySnap.docs)
    });