Reputation: 75
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
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));
Migrate from the Http service to the HttpClient. see migration guide
To update to
HttpClient
, you’ll need to replaceHttpModule
withHttpClientModule
from@angular/common/http
in each of your modules, inject theHttpClient
service, and remove anymap(res => res.json())
calls, which are no longer needed.
Upvotes: 6