3

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

2
  • What is the RxJS version you are using? Commented Oct 17, 2019 at 19:14
  • @Nikhil version 6.4.0 Commented Oct 17, 2019 at 19:26

1 Answer 1

4

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);
       })
    );
Sign up to request clarification or add additional context in comments.

4 Comments

The correct non-deprecated variant of combineLatest in RxJS 6 is the one that accepts arrays. combineLatest([a$, b$])
@martin - Looks like there are several overloads for this operator which are equally correct. I see both the variants in use in official docs, learn-rxjs, and in this migration guide.
@martin - Thanks for the info. Looks like the docs aren't updated. I didn't see any mention of it anywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.