我正在寻找角度为2的嵌套observable的解决方案。
我看到的大多数示例都涉及为初始请求返回的每个对象发出额外的http请求。在我的例子中,我想要对对象做出第一个请求,然后是第二个获取所有对象状态的请求,然后将它们连接在一起。
这是我到目前为止所做的:
getServicesAndStatuses(key: Key): Observable<Service[]> {
return this.getServices(key)
.flatMap((services: Service[]) => {
if (services.length > 0) {
return Observable.forkJoin(
this.getEnvironmentStatus(key).map((statuses: ServiceStatus[]) => {
services.forEach(service => {
statuses.forEach(status => {
if (status.virtualNetwork == key.virtualNetworkName && status.environment == key.environmentName && status.resource == service.resourceName && status.app == service.app.name) {
service.status = status;
}
});
if (service.status == null) {
service.status = new ServiceStatus();
}
console.log(service)
return service;
})
})
);
}
return Observable.of([]);
});
}
我相信我错过了return
因为console.log行正确显示了附加状态的服务对象。但我无法弄清楚我需要什么。
答案 0 :(得分:0)
我自己修好了:
getServicesAndStatuses(key: Key): Observable<Service[]> {
return this.getEnvironmentServices(key)
.flatMap((services: Service[]) => {
return this.getEnvironmentStatus(key).map((statuses: ServiceStatus[]) => {
services.forEach(service => {
statuses.forEach(status => {
if (status.virtualNetwork == key.virtualNetworkName && status.environment == key.environmentName && status.resource == service.resourceName && status.app == service.app.name) {
service.status = status;
}
});
if (service.status == null) {
service.status = new ServiceStatus();
}
return service;
});
return services;
})
});
}