275

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.

Quethzel Diaz
  • 621
  • 1
  • 11
  • 26
Malik Kashmiri
  • 5,741
  • 11
  • 44
  • 80

29 Answers29

482

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.

Edric
  • 24,639
  • 13
  • 81
  • 91
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Invalid module name in augmentation, module '../../Observable' cannot be found. Property 'map' does not exist on type 'Observable'. – Malik Kashmiri May 13 '16 at 12:33
  • 1
    How do you import the `Observable` class? Like this: `import {Observable} from 'rxjs/Observable';` or `import {Observable} from 'rxjs/Rx';`? – Thierry Templier May 13 '16 at 12:36
  • i am not import this class so far – Malik Kashmiri May 13 '16 at 12:41
  • Okay you use observables indirectly. I think that it's a problem related to your version of rxjs. See https://github.com/ocombe/ng2-translate/issues/81. Could you give versions of Angular2 and Rxjs you use? – Thierry Templier May 13 '16 at 12:44
  • angular 2.0.0-beta*17 and rxjs 25.0.0-beta7 – Malik Kashmiri May 13 '16 at 12:46
  • I think that you use rxjs 5.0.0-beta2 with angular2 beta17. – Thierry Templier May 13 '16 at 12:47
  • 1
    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. – Darshan Puranik May 31 '16 at 11:25
  • There have been more changes, see this page: https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md – CE_REAL May 07 '18 at 11:46
  • Yes, I'm using Angular6 and syntax from this post now works for me https://stackoverflow.com/questions/50527873/angular-6-property-map-does-not-exist-on-type-object – Hang Jul 15 '18 at 08:19
  • 4
    First write this command in vs code terminal of your project and restart the project. `npm install rxjs-compat` , than import the `map` operator. – Karan Raiyani Jul 26 '18 at 11:05
  • @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 –  May 11 '20 at 21:31
  • I have the operator imported but stil lsee the message when I run ng test – michalh Jul 28 '22 at 11:48
215

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

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Nick Hodges
  • 16,902
  • 11
  • 68
  • 130
  • 2
    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. – Joe May 08 '18 at 17:10
  • 1
    Thank you! You are a lifesaver!! Whoever is interested, the full reference to this change is available here: https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#howto-convert-to-pipe-syntax – malvadao May 08 '18 at 17:39
  • 32
    This worked for me, I also needed to add `import { map } from 'rxjs/operators';` – paul May 14 '18 at 23:57
  • Funny, this is exactly how we fixed it on angular 6 upgrade - which brought the latest rxjs too. – Max Jun 02 '18 at 18:07
  • 4
    @paul Thanks. Regarding `.catch`, we can use `catchError` like `.pipe(catchError(this.handleError))` ? – FindOutIslamNow Jun 06 '18 at 11:10
  • @FindOutIslamNow , thanks for answer about catchError. I use it after ı solved another problem :) – Nisanur Jul 14 '20 at 11:46
72

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';
d-cubed
  • 1,034
  • 5
  • 30
  • 58
Karan Raiyani
  • 883
  • 6
  • 13
40

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()));
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
Nadeem Qasmi
  • 2,217
  • 22
  • 16
  • it helped me @Katana24 as I saw the correct syntax with pipe. – punkologist Jun 12 '19 at 04:38
  • 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' – mrapi Jul 11 '20 at 13:25
  • 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 object? – Eric Nov 02 '20 at 09:17
  • This is the way to go with Angular 12, apparently. Thanks! – Sylvain Rodrigue Sep 03 '21 at 09:32
26

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';
S.Galarneau
  • 2,194
  • 1
  • 24
  • 26
  • 1
    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. – Sarun Intaralawan Oct 27 '17 at 14:34
20

In rxjs 6 map operator usage has been changed now you need to Use it like this.

import { map } from 'rxjs/operators';
myObservable
  .pipe(map(data => data * 2))
  .subscribe(...);

For reference https://www.academind.com/learn/javascript/rxjs-6-what-changed/#operators-update-path

  • 1
    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<{}>'` – atconway Jul 26 '18 at 17:45
12

In new version of httpClient module in angular, you have not yet to write it this way:

return this.http.request(request)
      .map((res: Response) => res.json());

but do it this way:

return this.http.request(request)
             .pipe(
               map((res: any) => res.json())
             );
mkamal
  • 199
  • 2
  • 6
10

In the latest Angular 7.*.*, you can try this simply as:

import { Observable, of } from "rxjs";
import { map, catchError } from "rxjs/operators";

And then you can use these methods as follows:

   private getHtml(): Observable<any> {
    return this.http.get("../../assets/test-data/preview.html").pipe(
      map((res: any) => res.json()),
      catchError(<T>(error: any, result?: T) => {
        console.log(error);
        return of(result as T);
      })
    );
  }

Check this demo for more details.

Hearen
  • 7,420
  • 4
  • 53
  • 63
8

just install rxjs-compat by typing in terminal:

npm install --save rxjs-compat

then import :

import 'rxjs/Rx';
Amir.S
  • 719
  • 8
  • 15
6
import { map } from "rxjs/operators";

getGetFunction(){
this.http.get('http://someapi')
.pipe(map(res => res));
}

getPostFunction(yourPara){
this.http.get('http://someapi',yourPara)
.pipe(map(res => res));
}

In above function you can see i didn't use res.json() since im using HttpClient. It applies res.json() automatically and returns Observable (HttpResponse < string>). You no longer need to call this function yourself after angular 4 in HttpClient.

Dushan
  • 1,365
  • 20
  • 26
5

In my case it wouldn't enough to include only map and promise:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';

I solved this problem by importing several rxjs components as official documentation recommends:

1) Import statements in one app/rxjs-operators.ts file:

// import 'rxjs/Rx'; // adds ALL RxJS statics & operators to Observable

// See node_module/rxjs/Rxjs.js
// Import just the rxjs statics and operators we need for THIS app.

// Statics
import 'rxjs/add/observable/throw';

// Operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/toPromise';

2) Import rxjs-operator itself in your service:

// Add the RxJS Observable operators we need in this app.
import './rxjs-operators';
Alex Valchuk
  • 623
  • 6
  • 8
  • 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 – Mohan Ram Sep 05 '17 at 04:51
4

In Angular v10.x and rxjs v6.x

First import map top of my service,

import {map} from 'rxjs/operators';

Then I use map like this

return this.http.get<return type>(URL)
  .pipe(map(x => {
    // here return your pattern
    return x.foo;
  }));
Arash
  • 1,692
  • 5
  • 21
  • 36
3

I had the same issue, I was using Visual studio 2015 which had an older version of typescript.

After upgrading the extension the issue got resolved.

Download Link

boxed__l
  • 1,334
  • 1
  • 10
  • 24
Naveen
  • 111
  • 1
  • 2
  • 8
3

I am using Angular 5.2 and when I use import 'rxjs/Rx'; it throws me the following lint error: TSLint: This import is blacklisted, import a submodule instead (import-blacklist)

See the screenshot below: Import of rxjs/Rx is blacklisted in Angular 5.2

SOLUTION: Solved it by importing only the operators that I needed. Example follows:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

So the fix would be to import specifically only the necessary operators.

Devner
  • 6,825
  • 11
  • 63
  • 104
  • @AndrewBenjamin I highly suggest that you do that. That will save you a lot of trouble down the lane. – Devner Mar 12 '18 at 09:15
3

I too faced the same error. One solution that worked for me was to add the following lines in your service.ts file instead of import 'rxjs/add/operator/map':

import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

For an example, my app.service.ts after debugging was like,

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

@Injectable()
export class AppService {
    constructor(private http: HttpClient) {}
    getData(): Observable<any> {
        return this.http.get('https://samples.openweathermap.org/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22')
        .pipe(map(result => result));
    }
}
Steffi Keran Rani J
  • 3,667
  • 4
  • 34
  • 56
3

Simply install rxjs-compat to solve the problem

npm i rxjs-compat --save-dev

And import it like below

import 'rxjs/Rx';
Ankit Bhatia
  • 92
  • 1
  • 7
3

First of all run installation as below:

npm install --save rxjs-compat@6

Now you need to import rxjs in service.ts:

import 'rxjs/Rx'; 

Voila! The problem has been fixed.

Alireza
  • 6,497
  • 13
  • 59
  • 132
3

simply run npm install --save rxjs-compat it fixes the error.

It is recommended here

trustidkid
  • 577
  • 4
  • 6
2

I tried all the possible answers posted above none of them worked,

I resolved it by simply restarting my IDE i.e., Visual Studio Code

May it helps someone.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
2

Use the map function in pipe function and it will solve your problem. You can check the documentation here.

this.items = this.afs.collection('blalal').snapshotChanges().pipe(map(changes => {
  return changes.map(a => {
    const data = a.payload.doc.data() as Items;
    data.id = a.payload.doc.id;
    return data;
  })
})
Kos
  • 4,890
  • 9
  • 38
  • 42
2

If after importing import 'rxjs/add/operator/map' or import 'rxjs/Rx' too you are getting the same error then restart your visual studio code editor, the error will be resolved.

1

for all those linux users that are having this problem, check if the rxjs-compat folder is locked. I had this exact same issue and I went in terminal, used the sudo su to give permission to the whole rxjs-compat folder and it was fixed. Thats assuming you imported

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch'; 

in the project.ts file where the original .map error occurred.

JavaJunior
  • 41
  • 6
1

npm install rxjs@6 rxjs-compat@6 --save

Ajay Takur
  • 6,079
  • 5
  • 39
  • 55
1

Thanks to https://github.com/nagamallabhanu

https://github.com/webmaxru/pwa-workshop-angular/issues/2#issuecomment-395024755

encapsulating

pipe(map(....))

worked

import { map } from 'rxjs/operators';

courseRef: AngularFireList<any>;
courses$: Observable<any[]>;

this.courseRef = db.list('/tags');
  this.courses$ = this.courseRef.snapshotChanges()
  .pipe(map(changes => {
      return changes.map(c => ({ key: c.payload.key, ...c.payload.val() 
  }));
 }));
 this.courses$.subscribe(res=>{
   console.log(res)
 })
D C
  • 708
  • 1
  • 6
  • 17
0
  import { Injectable } from '@angular/core';
  import { Http } from '@angular/http';
  import 'rxjs/add/operator/map';

  @Injectable({
  providedIn: 'root'
  })
  export class RestfulService {

  constructor(public http: Http) { }

 //access apis Get Request 
 getUsers() {
 return this.http.get('http://jsonplaceholder.typicode.com/users')
  .map(res => res.json());
  }

 }

run the command

 npm install rxjs-compat 

I just import

 import 'rxjs/add/operator/map';

restart the vs code, issue solved.

Nadeem Qasmi
  • 2,217
  • 22
  • 16
0

Angular 9:

 forkJoin([
  this.http.get().pipe(
    catchError((error) => {
      return of([]);
    })
  ),
  this.http.get().pipe(
    catchError((error) => {
      return of([]);
    })
  ),
Duc Nguyen
  • 530
  • 8
  • 16
0

If you are using this old way to get route params

 this._route.params
        .map(params => params['id'])

To updated it for new rxjs version

First, import the map from rxjs operators.

import { map } from 'rxjs/operators';

Second add pipe,

   this._route.params.pipe(
            map(params => params['id']))
Sunny
  • 1,286
  • 12
  • 16
0

i have same problem i solve it as follow

import { map } from 'rxjs/operators'; // imports statement 

return this.auth.user$.pipe(
  map(user =>{
    if(user) return true;
        this.router.navigate(['/login'], { queryParams: {returnUrl :state.url}});
    return false;
  }));
}

There was a problem with observable type which was null so i add type any and it helped to clear many errors

user$: Observable<firebase.User | any>;
smit agravat
  • 223
  • 2
  • 9
-1

The angular new version does not support .map you have to install this through cmd npm install --save rxjs-compat via this you can enjoy with old technique . note: don't forget to import these lines.

import { Observable, Subject } from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';