1
複数のhttp要求の後に要素を取得しようとしていますが、解決できない非同期の問題があります。私のサービスにサブスクライブ後のオブジェクト内の配列が未定義
Get関数::私のコードを投稿
get() {
return new Observable(project => {
this.channelsService.get().subscribe(
stations => {
this._stations = stations;
this._stations.forEach((station) => {
station.cssClass = this.channelsService.getCss(station.id.split('/').pop());
station.plstation$thumbnails = this.channelsService.getThumbnails(station.id.split('/').pop());
if (station.plstation$callSign !== '') {
const watchliveUrl = this.watchLiveApi + station.plstation$callSign + '/?schema=1.0.0';
this.http.get(watchliveUrl).subscribe(data => {
const body = data.json();
station.currentListing = body.currentListing;
station.nextListing = body.nextListing;
project.next(stations);
project.complete()
});
}
});
}, (error) => {
this.mapErrorService.mapError(error, 'Listing service (1)');
});
});
}
のget()を使用してサブスクライブ:
constructor(private listingService: ListingService) {
this.listingService.get().subscribe((stations) => {
this.stripDetails.channelList = stations;
// stations[6].currentListing Not undefined
console.log(stations);
// Now is undefined
console.log(stations[6].currentListing);
}); }
がどのように私はステーションを定義することができる[6] .currentListing ?
に、それは – goltornate
動作しない問題は、複数の要求をオフに発射 'forEach'ループを持っている、とあなたは(' project.completeを呼び出しているです) 'を返します。すべてのリクエストをまとめて、すべてが応答したときにのみ 'project'を完成させる必要があります。このために 'Rx.Observable.forkJoin()'を使うことができます。 – Duncan
どこにこの関数を置くのですか? – goltornate