1
votes

I'm upgrading an application from Angular 4 to Angular 5, and have the following code in a service

loadRefData(): Observable<RefData> {
    return this.participant$.switchMap((role) => {
      return this.http.get(`${this.API_URL}/${role}/participantGroups/refdata`)
        .map(refData => refData as RefData);
});

Since Angular 5 comes with the minimum requirement of RxJS 5.5.2, I'm converting it to use lettable operators with the .pipe() operator.

I thought I could do this

loadRefData(): Observable<RefData> {
    return this.participant$.switchMap((role) => {
      return this.http.get(`${this.API_URL}/${role}/participantGroups/refdata`)
        .pipe(
            map(refData => refData as RefData);
        );
});

but I get the error TypeError: this.participant$.switchMap is not a function. I get no compilation errors from the typescript compiler though.

this.participant$ is of type Observable<string>, and unless I'm mistaken I should be able to perform a switchMap on it.

What am I missing?

This is with typescript@^2.6.1, rxjs@^5.5.2.

EDIT: I just noticed that this.participant$ which is populated from ngrx\store.select is of type Store<string>.

1
import 'rxjs/add/operator/switchMap' - Eliseo
switchMap is an operator as well so you need to use it inside .pipe() like this.participant$.pipe(switchMap(role => ...)) - martin
@Eliseo this is RxJS 5 which means operators should be imported from 'rxjs/operators'. @martin That seems to have done the trick! I thought I had tried that already but who knows! Consider posting it as an answer. - Daniel B
I don't agree that we need .pipe(). I've never need it, but if works... - Eliseo
@Daniel B, thanks for the clarification. Always it's learned some new :) - Eliseo

1 Answers

-1
votes

Thats a quote taken from Angulars Blog, which might help you.

HttpClient

In version 4.3 we shipped HttpClient in @angular/common as a smaller, easier, and more powerful way to make web requests in Angular. The new HttpClient has gotten some great praise from developers, so we’re now recommending HttpClient for all applications, and deprecating the previous @angular/http library.

To update to HttpClient, you’ll need to replace HttpModule with HttpClientModule from @angular/common/http in each of your modules, inject the HttpClient service, and remove any map(res => res.json()) calls, which are no longer needed.

Source: Angular 5