J'essaie d'obtenir un élément après plusieurs requêtes http, mais il y a un problème d'asynch que je ne peux pas résoudre. Poster mon code:Array objet intérieur indéfini après abonnement
fonction Get dans mon service:
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() utilisé et abonnez-vous:
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);
}); }
Comment définir les stations [6] .currentListing ?
je modifier avec vos conseils, mais il ne fonctionne – goltornate
Le problème est que vous avez pas une boucle 'forEach' tir de demandes multiples, et vous appelez' project.complete () 'lorsque la première des requêtes http est terminée. Vous devez rassembler toutes les demandes ensemble et terminer seulement le «projet» quand ils ont tous répondu. Vous pouvez utiliser 'Rx.Observable.forkJoin()' pour cela. – Duncan
Où dois-je mettre cette fonction exactement? – goltornate