Property 'switchMap' does not exist on type 'Observable<User>'
Asked Answered
P

4

17

I have been getting the following error message when trying to apply the switchMap operator to my Observable:

Property 'switchMap' does not exist on type 'Observable'.

I'm currently using rxjs version 5.5.2, and in my component, I have it imported like so:

import 'rxjs/operator/switchMap';

However, I still get a compilation error. I have looked at similar questions and have not found a proper solution to this, any suggestions on what could be the issue here?

get appUser$() : Observable<AppUser> {
  return this.user$
    .switchMap(user => {
      if (user) return this.userService.get(user.uid);

      return Observable.of(null);
    });    
}

Image: enter image description here

Publicly answered 19/2, 2018 at 20:32 Comment(0)
K
20

You should be importing from rxjs/add/operator/switchMap if you're using the older "patch" style of operators:

import 'rxjs/add/operator/switchMap';

Since RxJS 5.5 with "pipable" operators you should import from 'rxjs/operators':

import { switchMap } from 'rxjs/operators';
Katti answered 19/2, 2018 at 20:34 Comment(1)
I have a problem when I pipe the (user => { }),Mccabe
F
23

I assume you're using Angular 6, which mean you're using the latest version of RxJS. You need to pipe your operators as

obs$.switchMap(() => { do stuff... });

change to

obs$.pipe(switchMap(() => { do stuff... }));

Edit: keep in mind that in your case, your observable is returned by this.refreshToken

this.refreshToken().pipe(switchMap(() => { do stuff... }));
Frivolous answered 17/3, 2019 at 6:2 Comment(0)
K
20

You should be importing from rxjs/add/operator/switchMap if you're using the older "patch" style of operators:

import 'rxjs/add/operator/switchMap';

Since RxJS 5.5 with "pipable" operators you should import from 'rxjs/operators':

import { switchMap } from 'rxjs/operators';
Katti answered 19/2, 2018 at 20:34 Comment(1)
I have a problem when I pipe the (user => { }),Mccabe
V
4

I think If you are using Angular 6 then you should use pipe

Version less than angular 6, the below code will works

        this.route.params.switchMap((data: Passengers) => 
        this.passengerService.getPassenger(data.id))
        .subscribe((data: Passengers) =>  this.passenger = data);

But for In Angular 6, you should pipe your operators

    this.route.params.pipe(switchMap((data: Passengers) => 
    this.passengerService.getPassenger(data.id)))
    .subscribe((data: Passengers) =>  this.passenger = data);
Valuation answered 5/7, 2019 at 6:37 Comment(0)
B
-1

we can avoid multiple nested subscribe using switchMap operator with pipe enter image description here

Barozzi answered 13/1, 2023 at 16:0 Comment(2)
Please edit to paste the text used in the image into your answer so that it can be read on all devices, edited, copied as text, and found through search. As it stands now, your image makes it hard to view and use your answer. See the formatting documentation for tips to make your text appear nicely without resorting to images.Ramayana
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Dmitri

© 2022 - 2024 — McMap. All rights reserved.