I have a problem with HTTP in Angular.
I just want to GET
a JSON
list and show it in the view.
im
this is happening because you are using the rxjs and in rxjs function are not static which means you can't call them directly you have to call the methods inside the pipe and import that function from the rxjs library
But if you are using rxjs-compat then you just need to import the rxjs-compat operators
The map you using here, is not the .map()
in javascript, it's Rxjs map function which working on Observables
in Angular...
So in that case you need to import it if you'd like to use map on the result data...
map(project: function(value: T, index: number): R, thisArg: any): Observable<R>
Applies a given project function to each value emitted by the source Observable, and emits the resulting values as an Observable.
So simply import it like this:
import 'rxjs/add/operator/map';
Plus what @mlc-mlapis commented, you're mixing lettable operators and the prototype patching method. Use one or the other.
For your case it should be
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
@Injectable()
export class SwPeopleService {
people$ = this.http.get('https://swapi.co/api/people/')
.map((res:any) => res.results);
constructor(private http: HttpClient) {}
}
https://stackblitz.com/edit/angular-http-observables-9nchvz?file=app%2Fsw-people.service.ts
For Angular versions 5 and above, the updated importing line looks like :
import { map } from 'rxjs/operators';
OR
import { map } from 'rxjs/operators';
Also these versions totally supports Pipable Operators so you can easily use .pipe() and .subscribe().
If you are using Angular version 2, then the following line should work absolutely fine :
import 'rxjs/add/operator/map';
OR
import 'rxjs/add/operators/map';
If you still encounter a problem then you must go with :
import 'rxjs/Rx';
I won't prefer you to use it directly bcoz it boosts the Load time, as it has a large number of operators in it (useful and un-useful ones) which is not a good practice according to the industry norms, so make sure you try using the above mentioned importing lines first, and if that not works then you should go for rxjs/Rx
With Angular 5 the RxJS import is improved.
Instead of
import 'rxjs/add/operator/map';
We can now
import { map } from 'rxjs/operators';
Since Http service in angular2 returns an Observable type, From your Angular2 installation directory('node_modules' in my case),We need to import map function of the Observable in your component using http service,as:
import 'rxjs/add/operator/map';