Angular 6 forkJoin not returning the data
Angular 6 forkJoin not returning the data
I am trying to make multiple request by using for loop and then using forkJoin to get all the data at once.
To get data, I have created one function in my service component and then inside for loop after calling that function I am subscribing it and then pushing the data into temporary variable. And after that when for loop is completed I am expecting forkJoin to subscribe and return the value.
Code:
const observables = ;
for (let i = 0; i < this.originalData.length; i++) {
this.dashboardService.getDetails(this.id1[i], this.id2[i]).subscribe(data => {
observables.push(data);
});
}
forkJoin(observables).subscribe(dataGroup => {
console.log(dataGroup);
});
If I check my network in inspect element then I can see that it is requesting those urls and the response is of 200 status but in console I don't see any error but at the same time there is no output from forkJoin subscribe.
I am new to this so may be I am not understanding it correctly how to use forkJoin.
Also I am using rxjs": "^6.0.0",
1 Answer
1
First create array of observables and then provide those observables to forkJoin
:
forkJoin
const observables = ;
for (let i = 0; i < this.originalData.length; i++) {
observables.push( this.dashboardService.getDetails(this.id1[i], this.id2[i])
};
forkJoin(...observables).subscribe(dataGroup => {
console.log(dataGroup);
});
@sive636, you needn't the spread operator, NOT forkJoin(...observables), just forkJoin(observables)
– Eliseo
2 days ago
@Eliseo Yes, you are correct. forkJoin accepts individual arguments as well as arrays.
– siva636
2 days ago
@siva636, observables is an array already, if you write forkJoin(...observables) you're copying the array and make the forkJoin, it's not necessary
– Eliseo
2 days ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks, this is the solution. So I need not to subscribe at for loop, will later accept it
– Shashank Sharma
2 days ago