Combine firestore queries with rxjs (OR query)

Viewed 533

I'm trying to combine two firestore queries in an Ionic/Angular project using RxJS. I found this blog post from which explains the basic principle, however, the .combineLatest method has been deprecated and I can't figure out how to pipe the two queries into a map instead.

caOrCoCities$: Observable<City[]>;

...

const californiaRef = this.angularFirestore
    .collection("cities", ref => ref.where("state","==","CA"));
const coloradoRef = this.angularFirestore
    .collection("cities", ref => ref.where("state","==","CO"));

Not sure how to update this below...

this.caOrCoCities$ = Observable
    .combineLatest(californiaRef.valueChanges(),
                   coloradoRef.valueChanges())
    .switchMap(cities => {
        const [californiaCities, coloradoCities] = cities;
        const combined = californiaCities.concat(coloradoCities);
        return Observable.of(combined);
    });

1 Answers

RxJS 6 had several changes in syntax. You can change your code as shown below.

import { combineLatest, of } from 'rxjs';

this.caOrCoCities$ = combineLatest(californiaRef.valueChanges(), coloradoRef.valueChanges())
    .pipe(
       switchMap(cities => {
         const [californiaCities, coloradoCities] = cities;
         const combined = californiaCities.concat(coloradoCities);
         return of(combined);
       })
    );
Related