Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mergeMap does not exist on type observable

I am trying to use mergeMap in rxjs6 and i am getting this error:

Property 'mergeMap' does not exist on type 'Observable<{}>'

I have tried import 'rxjs/add/operator/mergeMap'; and it is not working.

What am i doing wrong?


import {from, Observable} from 'rxjs';

export class Test {

    public doSomething(): Observable<any> {
        return from(...).mergeMap();
    }

}
like image 742
prolink007 Avatar asked May 02 '18 15:05

prolink007


3 Answers

That's correct, the "patch" style of operators has been removed since RxJS 6. You should better update your code to use only "pipeable" operators or install rxjs-compat package that provides backwards compatibility with RxJS 5.

For more detailed description see official doc: https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md

... more specifically this part: https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md#backwards-compatibility

like image 67
martin Avatar answered Oct 20 '22 22:10

martin


Thanks to the answer given by @martin, i was able to get it working with the new pipe operations in rxjs6. Here is my working code.

import {from, Observable} from 'rxjs';
import {mergeMap} from 'rxjs/operators';

export class Test {

    public doSomething(): Observable<any> {
        return from(...).pipe(mergeMap(...));
    }

}
like image 28
prolink007 Avatar answered Oct 21 '22 00:10

prolink007


Import the individual operators, then use pipe instead of chaining.

import { map, filter, catchError, mergeMap } from 'rxjs/operators';

source.pipe(
  map(x => x + x),
  mergeMap(n => of(n + 1, n + 2).pipe(
    filter(x => x % 1 == 0),
    scan((acc, x) => acc + x, 0),
  )),
  catchError(err => of('error found')),
).subscribe(printResult);

Source: https://auth0.com/blog/whats-new-in-rxjs-6/

like image 21
farrellw Avatar answered Oct 20 '22 23:10

farrellw