4

Here is my tech stack

Angular - 6.1.2

TypeScript - 2.7.2

rxjs - 6.2.2

Here is the code for my forkJoin method in Angular. Post requests are being called at the backend. But the params are not being passed.

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

        reqArray = [];

        for (let i = 0; i < this.array.length; i++) {

            if(array[i]==true)
            {
                let obj = { name: 'Test', status: array[i] };

                this.reqArray.push(this.http.post(url, { params: obj }).pipe(map((res: Response) => res)));

            }

      }




forkJoin(this.reqArray).subscribe(
       data => {


            console.log(data);

       },
       err => console.error(err)
    );

Tried this approach as well, but I get same response.

    Observable.forkJoin(this.reqArray).subscribe(
       data => {

             console.log(data);

       },
       err => console.error(err)
    );

When I pass data without the for loop in a static way, it works fine.

    forkJoin(
        this.http.get(url, { params: obj }).pipe(map((res: Response) => res)),
        this.http.get(url, { params: obj2 }).pipe(map((res: Response) => res)),
        this.http.get(url, { params: obj3 }).pipe(map((res: Response) => res)),
        this.http.get(url, { params: obj4 }).pipe(map((res: Response) => res))

    ).subscribe(
        data => {

            console.log(data);

        });

But, in my case I would have to create URLs array based on a few conditions, so adding it statically won't be possible.

What should I change in my code?

Anirudh
  • 2,767
  • 5
  • 69
  • 119
  • 1
    `forkJoin` support an array of Observables too so the problem will be somewhere else: https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/forkJoin.ts#L31 – martin Mar 08 '19 at 07:45

1 Answers1

2

You need to spread the array

forkJoin(...this.reqArray)
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
  • I had a very similar scenario but spread resulted in a lint issue. https://stackoverflow.com/q/52486786/2050306 – robert Mar 08 '19 at 07:40