GAJANAN JADHAV
GAJANAN JADHAV

Reputation: 75

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

I am trying to get data from mongodb, for which I have written a service. But I am getting an error like error TS2339: Property 'map' does not exist on type 'Observable<Response>'

Please help me to resolve this error...

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class DataService {

  result:any;

  constructor(private _http: Http) { }

  getUsers() {
    return this._http.get("/api/users")
      .map(result => this.result = result.json().data);
  }

}

Upvotes: 3

Views: 3603

Answers (1)

Kim Kern
Kim Kern

Reputation: 60527

You have to import and use the map operator differently:

Change

import 'rxjs/add/operator/map';

to

import { map } from 'rxjs/operators';

Then, do

return this._http.get("/api/users")
      .pipe(map(result => this.result = result.json().data));

Addiontal suggestion from Vikas

Migrate from the Http service to the HttpClient. see migration guide

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.

Upvotes: 6

Related Questions