Error rxjs_Observable__.Observable.forkJoin is not a function?
Asked Answered
A

4

18

I am using Rxjs in an angualr-cli application.

in viewer.component.ts

    //Other Imports
    import { Observable } from 'rxjs/Observable';
    //omitting for brevity
   export class ViewerComponent implements OnInit, AfterViewInit, OnDestroy {
    someFunction(someArg){
    //omitting for brevity
    let someArray: any = [];
    //Add some info
    Observable.forkJoin(someArray).subscribe(data => {
              //Do something with data
            });
    }
    //omitting for brevity
    }  

I get and error as

ERROR TypeError: __WEBPACK_IMPORTED_MODULE_2_rxjs_Observable__.Observable.forkJoin is not a function
    at ViewerComponent.webpackJsonp../src/app/component/viewer.component.ts.ViewerComponent.someFunction(http://localhost:4200/main.bundle.js:4022:73)
    at http://localhost:4200/main.bundle.js:3951:31

But if I Import Rxjs completely (import 'rxjs';) everything works. No error. I cont seem to understand whats additional that needed. I also tries importing rxjs/Observable/forkjoin but nothing to avail.

Any pointers on how to go about this.

Aboveground answered 25/6, 2017 at 6:32 Comment(4)
what do you mean by import completly?Sulamith
I have updated the question. Completely means import 'rxjs';Aboveground
what is someArray ? what it contains ?Underneath
don't import whole lib, import necessary pieces insteadDakotadal
T
31

As explained here, you have two options:

  1. Either import all operators as a single package
  2. Or import each operator individually

In the first case you would use import like this:

import Rx from 'rxjs/Rx';

Rx.Observable.forkJoin(1,2,3)

In the second like this:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';

I believe what you're looking for is the second option.

Treasonable answered 25/6, 2017 at 6:51 Comment(3)
Thank Maximus, I think best practice is the 2nd option?Dakotadal
Thanks, Worked like charm :)Aboveground
The first option was the solution for me. Thanks!Underwaist
P
1

For RxJS v6.5+, it is now:

import { forkJoin } from 'rxjs';

https://www.learnrxjs.io/operators/combination/forkjoin.html

Perse answered 30/10, 2019 at 20:49 Comment(0)
D
1

Use the forkJoin Operator as a function by importing forkJoin instead of importing Observable.
Use forkJoin.subscribe() in place of Observable.forkJoin.subscribe()

import { forkJoin } from 'rxjs/observable/forkJoin'

export class ViewerComponent implements OnInit, AfterViewInit, OnDestroy {
    someFunction(someArg){
      let someArray: any = [];
      forkJoin(someArray).subscribe(data => {
      });
    }
}

Source Code : https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/forkJoin.ts

Discrown answered 21/4, 2020 at 10:12 Comment(0)
R
0

In one project I used the following forkjoin code to upload images, it works perfectly.

/**
       *  Upload multiple image and then update the feature
       */
      public uploadImages(apiParams: any[], imageItems: any[]): Observable<any> {
        if (imageItems.length === 0) return;

        let promises = [];
        for (let imageItem of imageItems) {
          promises.push(this.uploadImage(apiParams, imageItem));
        }
        return Observable.forkJoin(...promises);
      }

      /**
       * upload single image
       */
      public uploadImage(apiParams: any[], imageItem: any): any {
        let _formData = new FormData();
        _formData.append("UploadedImage", imageItem,  (new Guid()).toString()+'.png');
        const imageApiUrl = StringFormatter.format(imagePoints.create, apiParams);
        return Observable.fromPromise(
          fetch(imageApiUrl, {
            method: "POST",
            body: _formData
          })
          .then(result => result.json())
        );
      }
Rosenblum answered 17/1, 2019 at 22:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.