While iterating through an array and invoking the LoadReportsData
function, I noticed that the calls to getProjectReportsData
within _reportingService
are made in the correct sequence. However, upon returning from the service map and then using .subscribe()
, the code inside it is not executed until all the calls are completed.
As a result, the data is assigned to this.reportData
in a random order. This randomness leads to the creation of worksheets in the workbook in a haphazard manner instead of following the intended order. I am seeking suggestions for alternative methods to handle these calls or any workarounds that could mitigate this issue.
FetchProjectSubmissionReportingAssets(ID: number,
mySelectiontot: number, PRsDelimitedList: string, StartDate: string, EndDate: string) {
let currAsset: string = '';
let ID: number;
var fetchAssetData = {
'CSID': ID
}
this._reportingService.isLoading = true;
this._reportingService.getProjSubRptAssets(fetchAssetData).pipe(
map(result => {
if (result != null) {
for (var i = 0; i < result.length; i++) {
this.ProjReqSubmissionReportingAssets = result;
currAsset = result[i].option;
ID = result[i].id;
this.LoadReportsData(ID, currAsset, i);
}
}
}))
.subscribe();
}
LoadReportsData(CSAsID: number, currAsset: string, tabIndex: number) {
this.wb = XLSX.utils.book_new();
var indata = {
'CSID': this.CSID,
'CSAsID': CSAsID,
'StartDate': this.StartDate,
'EndDate': this.EndDate,
'PRsDelimitedList': this.PRsDelimitedList
}
this._reportingService.getProjectReportsData(indata).pipe(
map(result => {
this.reportData = result;
this.idx = this.idx + 1;
if (this.reportData != null) {
this.ws = this.ws + '_' + tabIndex.toString();
this.ws = XLSX.utils.json_to_sheet(this.reportData);
XLSX.utils.book_append_sheet(this.wb, this.ws, currAsset);
console.log(currAsset);
}
if (this.ProjReqSubmissionReportingAssets.length == this.idx) {
var currDateTime = this.fn_getTimeStamp();
XLSX.writeFile(this.wb, "ProjReport_" + this.HCs + "_" + currDateTime + ".xlsx");
}
}))
.subscribe();
this._reportingService.isLoading = false;
}