drupalphil
drupalphil

Reputation: 91

Dynamic Querying in AngularFirestore2 with snapshotChanges()

Using the example provided by the AngularFire2 docs I'm able to set a dynamic query that returns an observable of valueChanges().

Example found here: https://github.com/angular/angularfire2/blob/master/docs/firestore/querying-collections.md

How does one accomplish the same thing but with snapshotChanges()?

I'm looking to get the document id associated with each document.

Upvotes: 0

Views: 2159

Answers (1)

drupalphil
drupalphil

Reputation: 91

::facepalm::

Seems it's just the same as streaming as normal. For those interested anyhow:

this.sizeFilter$ = new BehaviorSubject(null);
    this.colorFilter$ = new BehaviorSubject(null);
    this.items$ = Observable.combineLatest(
      this.sizeFilter$,
      this.colorFilter$
    ).switchMap(([size, color]) => 
      afs.collection('items', ref => {
          let query : firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
          if (size) { query = query.where('size', '==', size) };
          if (color) { query = query.where('color', '==', color) };
          return query;
      }).snapshotChanges()
          .map(actions => {
              return actions.map(a => {
                  const data = a.payload.doc.data() as any;
                  const id = a.payload.doc.id;
                  return {id, ...data};
              });
          })
    );

Upvotes: 3

Related Questions