Angular HTTP GET with TypeScript error http.get(...).map is not a function in [null]
I think that you need to import this:
import 'rxjs/add/operator/map'
Or more generally this if you want to have more methods for observables. WARNING: This will import all 50+ operators and add them to your application, thus affecting your bundle size and load times.
import 'rxjs/Rx';
See this issue for more details.
Just some background... The newly minted Server Communication dev guide (finally) discusses/mentions/explains this:
The RxJS library is quite large. Size matters when we build a production application and deploy it to mobile devices. We should include only those features that we actually need.
Accordingly, Angular exposes a stripped down version of
Observable
in therxjs/Observable
module, a version that lacks almost all operators including the ones we'd like to use here such as themap
method.It's up to us to add the operators we need. We could add each operator, one-by-one, until we had a custom Observable implementation tuned precisely to our requirements.
So as @Thierry already answered, we can just pull in the operators we need:
import 'rxjs/add/operator/map';
import 'rxjs/operator/delay';
import 'rxjs/operator/mergeMap';
import 'rxjs/operator/switchMap';
Or, if we're lazy we can pull in the full set of operators. WARNING: this will add all 50+ operators to your app bundle, and will effect load times
import 'rxjs/Rx';
From rxjs 5.5 onwards, you can use the pipeable operators
import { map } from 'rxjs/operators';
What is wrong with the import 'rxjs/add/operator/map';
When we use this approach map
operator will be patched to observable.prototype
and becomes a part of this object.
If later on, you decide to remove map
operator from the code that handles the observable stream but fail to remove the corresponding import statement, the code that implements map
remains a part of the Observable.prototype
.
When the bundlers tries to eliminate the unused code (a.k.a. tree shaking
), they may decide to keep the code of the map
operator in the Observable even though it’s not being used in the application.
Solution - Pipeable operators
Pipeable operators are pure functions and do not patch the Observable. You can import operators using the ES6 import syntax import { map } from "rxjs/operators"
and then wrap them into a function pipe()
that takes a variable number of parameters, i.e. chainable operators.
Something like this:
getHalls() {
return this.http.get(HallService.PATH + 'hall.json')
.pipe(
map((res: Response) => res.json())
);
}