简体   繁体   中英

Combine firestore queries with rxjs (OR query)

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);
    });

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);
       })
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM