Search code examples
typescriptfirebasegoogle-cloud-firestoreangularfire2

Obtain data from QuerySnapshot


I want to obtain data from my cloud firestore. The collection is called 'feedback'. The following code is working

  constructor(private fireStore: AngularFirestore) {
    this.fireStore.collection('feedback')
                  .get()
                  .subscribe((item: firebase.firestore.QuerySnapshot) => {
      this.feedbackItems = item.docs.map((dataItem: firebase.firestore.QueryDocumentSnapshot) => dataItem.data());
    });
  }

with the import

import { AngularFirestore } from '@angular/fire/firestore';

However this.feedbackItems is now an array of my feedback object from the cloud firestore. Is it possible to obtain the results as an observable? What is the professional way to obtain the data?

I found several solutions on stackoverflow, however some functions were not available (I guess because a different library or JavaScript is used?)


Solution

  • Is it possible to obtain the results as an observable?

    You can import map from the rxjs/operators to alter the response. You can then assign it to some observable.

    this.feedbackItemObservable = this.fireStore.collection('feedback')
      .get()
      .pipe(map((item:firebase.firestore.QuerySnapshot) => {
        return item.docs.map((dataItem: firebase.firestore.QueryDocumentSnapshot) => dataItem.data());
      }));