Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can no longer use forkjoin with rxjs 5.5.2

Recently, I decided to migrate a big project that has been created from scratch with the "Quick Start" of Angular to a version using Angular CLI 1.5.5. Right now, I'm fixing the different issues that arise and I can't get to fix this one.

I read that it was better to use lettable operators of rxjs, which I did and it works very well. However, I also have these lines of code:

import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/forkjoin";

...

let piecesGrouping$: Observable<IGroupedPiece[]>[] = deliveries.map(delivery => this._pieceService.getGroupedPieces(delivery.pieces));

Observable
  .forkJoin(...piecesGrouping$)
  .subscribe((groups) => {
    groups.forEach((group, i) => deliveries[i].groupedPieces = group);

    resolve();
  });

They were working very well in the previous version that was using rxjs 5.4.3, now with rxjs 5.5.2, they don't anymore and I get the following error:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'apply' of undefined TypeError: Cannot read property 'apply' of undefined

I tried to replace the spread operator by piecesGrouping$[0], piecesGrouping$[1] as the error does not say anything about forkJoin in itself, then I get:

ERROR Error: Uncaught (in promise): TypeError: WEBPACK_IMPORTED_MODULE_8_rxjs_Observable.a.forkJoin is not a function TypeError: WEBPACK_IMPORTED_MODULE_8_rxjs_Observable.a.forkJoin is not a function

So it looks like that I'm importing forkJoin the wrong way. I tried to import it from "rxjs/observable/forkJoin" but it didn't work either.

What am I missing?

like image 738
ssougnez Avatar asked Dec 12 '17 20:12

ssougnez


People also ask

Is RxJS forkJoin deprecated?

ForkJoin method signature has changedThis is now deprecated and you must pass an array of observables to forkJoin operator.

What can I use instead of forkJoin?

concat() which will handle each observable in sequence.

Does forkJoin cancel?

Overall, in order for forkJoin to emit a value, all given observables have to emit something at least once and complete. If any given observable errors at some point, forkJoin will error as well and immediately unsubscribe from the other observables.

What will happen if error happens in forkJoin?

The forkJoin will subscribe to the passed observables, by making the HTTP GET requests. If any input observable errors at some point, forkJoin will find the error as well and all other observables will be immediately unsubscribed.


2 Answers

Don't use Observable patching, use forkJoin directly:

import {forkJoin} from "rxjs/observable/forkJoin";

forkJoin(...piecesGrouping$).subscribe()

Check this resource to learn more about forkJoin.

like image 79
Max Koretskyi Avatar answered Nov 15 '22 23:11

Max Koretskyi


RxJS 6.x uses this import

import {forkJoin} from 'rxjs';

As stated in the other answers, it still needs to be used as a function as it now stands on its own. Docs are here

like image 21
Beanwah Avatar answered Nov 15 '22 23:11

Beanwah