I try to chain multiple rx.js observables and pass the data. Flatmap
should be the fitting operator but with an import of
import { Observable } from 'rxjs/Observable';
it is not found:
Error TS2339: Property 'flatmap' does not exist on type 'Observable<Coordinates>'
Version 5.0.0-beta.6
of rx.js is used.
public getCurrentLocationAddress():Observable<String> {
return Observable.fromPromise(Geolocation.getCurrentPosition())
.map(location => location.coords)
.flatmap(coordinates => {
console.log(coordinates);
return this.http.request(this.geoCodingServer + "/json?latlng=" + coordinates.latitude + "," + coordinates.longitude)
.map((res: Response) => {
let data = res.json();
return data.results[0].formatted_address;
});
});
}
It turns out the answer is quite simple:
the operator is called
mergeMap
in this version of rxjsEDIT:
Furthermore, you might have to use
import 'rxjs/add/operator/mergeMap'
.With RxJS 5.5+, the
flatMap
operator has been renamed tomergeMap
. Instead, you should now use themergeMap
operator in conjunction withpipe
.You can still use flatMap using the alias
FlatMap
.For each RxJS Operator you import, including
mergeMap
, you should now import from 'rxjs/operators' and use the pipe operator.Example of using mergeMap on an Http request Observable
Notice here that
flatMap
is replaced withmergeMap
and thepipe
operator is used to compose the operators in similar manner to what you're used to with dot-chaining.See the rxjs documentation on lettable operators for more info https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md
Correct import should look like below:
Importing the module
mergeMap
will enable you to useflatMap
in your codeWhen you will import in your code
import { Observable } from 'rxjs/Rx';
, additionalmergeMap
import is not needed but you can expect errors during the AoT compilation.In my case I needed to import the augmentation for mergeMap:
As flatMap is an alias of mergeMap, importing the module above will enable you to use flatMap.
It worked for me!