2017-12-24 5 views
-2

私はそれを見ると簡単な質問がありますが、Rxで十分な経験がありません。Observable内の複数のオブザーバブルが期待どおりに解決されていない

ngOnInit() { 
this.agentService.getAllAgents().subscribe((agents: Agent[]) => { 
    this.agents = agents.sort((x: Agent, y: Agent) => 
    Number(new Date(x.date)) - Number(new Date(y.date))); 

    for (const agent of this.agents) { 
    forkJoin(this.geoLocator.find(agent.address)).subscribe(results => { 
     for (const coordinates of results) { 
     agent.distance = this.getDistance(<Coordinates>environment.google.COORDINATES, coordinates, Unit.km); 
     } 
    }); 
    } 
    console.log(this.agents[0]); 
    console.log(JSON.stringify(this.agents[0]); 
}); 

}

enter image description here

これは私が得る結果です。

質問はどのように、なぜですか?

+0

避けネストされたサブスクライブJota.Toledo @ –

+0

あなたは何を示唆していますか? – IamStalker

+0

https://stackoverflow.com/questions/42888604/rxjs-observables-nested-subscriptions –

答えて

0

public find(agent: Agent): Observable<Agent> { 
 
    return this.http.get<Agent>(`${environment.google.BASE_URL}${agent.address}`) 
 
     .map(response => { 
 
     const coordinates = <Coordinates> { 
 
      latitude: response['results'][0].geometry.location.lat, 
 
      longitude: response['results'][0].geometry.location.lng 
 
     }; 
 
     agent.distance = this.getDistance(<Coordinates>environment.google.COORDINATES, coordinates, Unit.km); 
 
     return agent; 
 
     }); 
 
    }

関連する問題