Angular foreach和subscribe内部的同步网络请求
我需要的是从一个api获取数据,然后将其发送到另一个api并附加值 我尝试过使用async await,但没有成功,我认为由于嵌套类型,它不起作用Angular foreach和subscribe内部的同步网络请求,angular,rxjs,angular8,Angular,Rxjs,Angular8,我需要的是从一个api获取数据,然后将其发送到另一个api并附加值 我尝试过使用async await,但没有成功,我认为由于嵌套类型,它不起作用 //service.ts getRecentBackups(); { return this.http.get<any[]>( `${environment.URL}` ); } getAccounts(accountId); { return this.http.get( `${environment.UR
//service.ts
getRecentBackups();
{
return this.http.get<any[]>(
`${environment.URL}`
);
}
getAccounts(accountId);
{
return this.http.get(
`${environment.URL}/${accountId}`
);
}
//component.ts
this.abc.getRecentBackups().subscribe(data => {
const backupData = data['activities'];
backupData.forEach(activity => {
const alert = new Alert();
//Problem here need to extract response first
let response = this.abc.getAccounts(activity['accountNum']);
this.longRunningAlerts.push(alert);
this.backupAlerts.push(alert);
});
//this should execute after above is done.
this.totalRecentStatus = backupData.length;
this.longRunningAlerts = [...this.longRunningAlerts];
this.backupAlerts = [...this.backupAlerts];
}
//service.ts
getRecentBackup();
{
返回this.http.get
在控制台中返回Observable。试试这个
//service.ts
getRecentBackups();
{
return this.http.get<any[]>(
`${environment.URL}`
);
}
getAccounts(accountId);
{
return this.http.get(
`${environment.URL}/${accountId}`
);
}
//component.ts
this.appService.getRecentBackups().pipe(
map(data => data['activities']),
map(backupData => backupData.map(activity => {
return this.appService.getAccounts(activity['accountNum']).pipe(
map(res => ({ activity: activity, account: res }))
)
})),
switchMap(apiRequest => zip(...apiRequest))
).subscribe(result => {
// this.backupAlerts.push({ activity: activity, account: response });
console.log(result);
this.backupAlerts = [...this.backupAlerts];
console.info('Backup Alerts', this.backupAlerts)
})
//service.ts
getRecentBackup();
{
返回this.http.get
当从一个可观察值切换到下一个可观察值时,你需要使用开关映射。你能提供一些细节吗?你能看看@reactular开关映射中的一个错误吗类型为“void”的“this”上下文不能分配给类型为“observable”的方法的“this”上下文。
@Napster你能在stackblitz和sha中创建一个例子吗re.给你,先生,请把我拉出来。@Napster更新了示例。现在你将收到这两个值