Property 'map' does not exist on type 'Observable<Response>'

385,665

Solution 1

You need to import the map operator:

import 'rxjs/add/operator/map'

Or more generally:

import 'rxjs/Rx';

Notice: For versions of RxJS 6.x.x and above, you will have to use pipeable operators as shown in the code snippet below:

import { map } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';

// ...
export class MyComponent {
  constructor(private http: HttpClient) { }
  getItems() {
    this.http.get('https://example.com/api/items').pipe(map(data => {})).subscribe(result => {
      console.log(result);
    });
  }
}

This is caused by the RxJS team removing support for using See the breaking changes in RxJS' changelog for more info.

From the changelog:

operators: Pipeable operators must now be imported from rxjs like so: import { map, filter, switchMap } from 'rxjs/operators';. No deep imports.

Solution 2

Revisiting this because my solution isn't listed here.

I am running Angular 6 with rxjs 6.0 and ran into this error.

Here's what I did to fix it:

I changed

map((response: any) => response.json())

to simply be:

.pipe(map((response: any) => response.json()));

I found the fix here:

https://github.com/angular/angular/issues/15548#issuecomment-387009186

Solution 3

Just write this command in the VS Code terminal of your project and restart the project.

npm install rxjs-compat

You need to import the map operator by adding this:

import 'rxjs/add/operator/map';

Solution 4

For the Angular 7v

Change

import 'rxjs/add/operator/map';

To

import { map } from "rxjs/operators"; 

And

 return this.http.get('http://localhost/ionicapis/public/api/products')
 .pipe(map(res => res.json()));

Solution 5

I had the same issue with Angular 2.0.1 because I was importing Observable from

import { Observable } from 'rxjs/Observable';

I resolve my problem on importing Observable from this path instead

import { Observable } from 'rxjs';
Share:
385,665

Related videos on Youtube

Malik Kashmiri
Author by

Malik Kashmiri

Updated on April 22, 2022

Comments

  • Malik Kashmiri
    Malik Kashmiri about 2 years

    I am trying to call an API from Angular but am getting this error:

    Property 'map' does not exist on type 'Observable<Response>'

    The answers from this similar question didn't solve my issue: Angular 2 beta.17: Property 'map' does not exist on type 'Observable<Response>'.

    I am using Angular 2.0.0-beta.17.

  • Malik Kashmiri
    Malik Kashmiri about 8 years
    Invalid module name in augmentation, module '../../Observable' cannot be found. Property 'map' does not exist on type 'Observable<Response>'.
  • Thierry Templier
    Thierry Templier about 8 years
    How do you import the Observable class? Like this: import {Observable} from 'rxjs/Observable'; or import {Observable} from 'rxjs/Rx';?
  • Malik Kashmiri
    Malik Kashmiri about 8 years
    i am not import this class so far
  • Thierry Templier
    Thierry Templier about 8 years
    Okay you use observables indirectly. I think that it's a problem related to your version of rxjs. See github.com/ocombe/ng2-translate/issues/81. Could you give versions of Angular2 and Rxjs you use?
  • Malik Kashmiri
    Malik Kashmiri about 8 years
    angular 2.0.0-beta*17 and rxjs 25.0.0-beta7
  • Thierry Templier
    Thierry Templier about 8 years
    I think that you use rxjs 5.0.0-beta2 with angular2 beta17.
  • Darshan Puranik
    Darshan Puranik about 8 years
    I am using Angular 2 RC1 and rxjs 5.0.0-beta2. I have imported Observable as import {Observable} from 'rxjs/observable'; and imported map operator like import 'rxjs/add/operator/map'; It was working when I was using Beta 7. I just upgraded to RC1 and it broke.
  • Mohan Ram
    Mohan Ram almost 7 years
    I feel its not right to include rxjs-operators in every services. It should be included only at app.module.ts but unfortunately ng test throws error if operators are not imported in specific places
  • Sarun Intaralawan
    Sarun Intaralawan over 6 years
    This practice is considered not bundle-size-friendly since that statement imports all operators of Observable (including the ones that you don't use) into the bundle. Instead, you should import each operator individually. I recommend using "lettable operators", which was introduced in RxJS v5.5 to support better tree-shaking.
  • Devner
    Devner over 6 years
    @AndrewBenjamin I highly suggest that you do that. That will save you a lot of trouble down the lane.
  • CE_REAL
    CE_REAL about 6 years
    There have been more changes, see this page: github.com/ReactiveX/rxjs/blob/master/MIGRATION.md
  • Joe
    Joe about 6 years
    I'm not sure why the migration tool rxjs-5-to-6-migrate doesn't pick this up? It's interesting that it doesn't address the do/tap re-mapping which in my environment is still unresolved even though the import tool recognizes it. 90% of my 'unanticipated wasted time' in Angular development is spent with RxJS document and code boundary issues, this is really frustrating.
  • malvadao
    malvadao about 6 years
    Thank you! You are a lifesaver!! Whoever is interested, the full reference to this change is available here: github.com/ReactiveX/rxjs/blob/master/…
  • paul
    paul about 6 years
    This worked for me, I also needed to add import { map } from 'rxjs/operators';
  • Max
    Max about 6 years
    Funny, this is exactly how we fixed it on angular 6 upgrade - which brought the latest rxjs too.
  • FindOutIslamNow
    FindOutIslamNow about 6 years
    @paul Thanks. Regarding .catch, we can use catchError like .pipe(catchError(this.handleError)) ?
  • Hang
    Hang almost 6 years
    Yes, I'm using Angular6 and syntax from this post now works for me stackoverflow.com/questions/50527873/…
  • Karan Raiyani
    Karan Raiyani almost 6 years
    First write this command in vs code terminal of your project and restart the project. npm install rxjs-compat , than import the map operator.
  • atconway
    atconway almost 6 years
    Take note of this answer as anyone doing RxJS v6 will need to use the .pipe() or none of the operators will work directly off the Observable. You'll see an error like, Property 'share' does not exist on type 'Observable<{}>'
  • Ajay Takur
    Ajay Takur almost 6 years
    if it does not work close all the files and restart the vs studio.
  • punkologist
    punkologist about 5 years
    it helped me @Katana24 as I saw the correct syntax with pipe.
  • Tom Faltesek
    Tom Faltesek almost 5 years
    This late answer is redundant.
  • trustidkid
    trustidkid over 4 years
    Please don't forget to import 'rxjs/add/operator/map'
  • Giacomo Brunetta
    Giacomo Brunetta about 4 years
    @ThierryTemplier Using Angular9 the same code of yours (changing url of course) it give me 'undefined'. Url return a json result if you try to put on browser
  • mrapi
    mrapi almost 4 years
    that solution works good in angular 10 where not need to install rxjs-compat that will give warnings like 'Fix CommonJS or AMD dependencies can cause optimization bailouts'
  • Nisanur
    Nisanur almost 4 years
    @FindOutIslamNow , thanks for answer about catchError. I use it after ı solved another problem :)
  • Eric
    Eric over 3 years
    How do you access the JSON object through this? When i try to print the JSON string with console.log(), I only get a Observable<Any> object?
  • Nick Patterson
    Nick Patterson almost 3 years
    This is the way to go with Angular 12, apparently. Thanks!