我对 Angular 和 RxJS 非常陌生,我习惯使用 Promise。我需要执行相当于 Promise.All() 的操作,我有一个项目数组,每个项目都需要进行 HTTP 调用,并且这些项目中的每个项目都有一个项目数组,每个项目都需要进行 HTTP 调用。我之前只将 forkJoin 用于单个数组,并且工作正常,但现在我将 forkJoin 嵌套在 forkJoin 中,并且在需要流时不断收到错误 Youprovid“undefined”。所有 API 调用都返回 Observables,那么它可能是 forkJoins 本身之一的结果吗?这是我的代码。
.updateTrackSystemCompact({ ...uploadTrackSystemCompact, trackIds: null })
.subscribe((uploadInspectionJob: UploadInspectionJob) => {
uploadTrackSystemCompact = {
id: uploadTrackSystem.id,
country: uploadTrackSystem.country,
standardsUsed: uploadTrackSystem.standardsUsed,
region: uploadTrackSystem.region,
company: uploadTrackSystem.company,
systemId: uploadTrackSystem.systemId,
postalCode: uploadTrackSystem.postalCode,
address: uploadTrackSystem.address,
city: uploadTrackSystem.city,
state: uploadTrackSystem.state,
createdAt: uploadTrackSystem.createdAt,
updatedAt: uploadTrackSystem.updatedAt,
inspectionReport: uploadTrackSystem.inspectionReport,
inspectionReports: uploadTrackSystem.inspectionReports,
trackIds: trackIndexes,
};
this.trackSystemService
.doUploadS3TrackSystemCompact(uploadTrackSystemCompact, uploadInspectionJob.uploadUrl)
.subscribe((resp) => {
if (resp === undefined) {
let alert = this.alertCtrl.create({
title: "S3 response was undefined",
message: `response status is ${resp.status}`,
buttons: ["OK"],
});
alert.present();
}
if (resp !== undefined) {
forkJoin(
uploadTrackSystem.tracks.map((track, trackIndex) => {
const inspectionJobId = uploadInspectionJob.id;
forkJoin(
track.assets.map((asset, assetIndex) => {
const request: UploadCompactRequestAsset = {
inspectionJobId: inspectionJobId,
trackIndex: trackIndex,
assetIndex: assetIndex,
};
this.trackSystemService
.getAssetUploadUrl(request)
.subscribe((response: UploadCompactResponse) => {
const url = response.uploadUrl;
this.trackSystemService.uploadS3Asset(asset, url);
});
})
).subscribe(() => {
const request: UploadCompactRequestTrack = {
inspectionJobId: inspectionJobId,
trackIndex: trackIndex,
};
this.trackSystemService
.getTrackUploadUrl(request)
.subscribe((response: UploadCompactResponse) => {
const url = response.uploadUrl;
const uploadTrackCompact: TrackCompact = {
id: track.id,
uuid: track.uuid,
trackId: track.trackId,
inspectionId: track.inspectionId,
trackSystemId: track.trackSystemId,
start: track.start,
end: track.end,
trackReport: track.trackReport,
trackReports: track.trackReports,
assetIds: track.assets.map((asset, index) => {
return index;
}),
};
this.trackSystemService.uploadS3TrackCompact(uploadTrackCompact, url);
});
});
})
).subscribe(() => {
this.trackSystemService.setReadyStatus(uploadInspectionJob.id).subscribe(
(inspectionJob: InspectionJob) => {
this.dismissLoading();
// this.uploadButtonDisabled = false;
this.awaitInspectionProcessing();
},
(err) => {
this.increaseFailedUploadCount();
this.uploadButtonDisabled = false;
this.dismissLoading();
switch (err.status) {
case 401:
Utils.sessionExpiredAlert(this.authService, this.alertCtrl, this.navCtrl);
break;
default:
let alert = this.alertCtrl.create({
title: "Error",
subTitle: `There was an error setting the ready status: (${err.status})`,
buttons: ["OK"],
});
alert.present();
break;
}
}
);
});
} else {
this.increaseFailedUploadCount();
this.uploadButtonDisabled = false;
this.dismissLoading();
let alert = this.alertCtrl.create({
title: "Error",
subTitle: "There was an error uploading to the S3",
buttons: ["OK"],
});
alert.present();
}
});
});
});```
您可以使用
使您的代码更具可读性uploadTrackSystemCompact = {
...uploadTrackSystem
trackIds: trackIndexes,
};
const uploadTrackCompact: TrackCompact = {
...track
assetIds: track.assets.map((asset, index) => {
return index;
}),
};