I am trying to get multiple person object from multiple API endpoint.
1st API endpoint returns the names of people. 2nd API endpoint returns person detail information. So I have to do get names first, then loop through each name to get detailed information.
Let's say API's person JSON response will be like: {name:'brad', age:'43'}
I have person.component.ts:
getPersons(): void {
this.personService.getPeople()
.subscribe(people => this.people = people);
}
I have person.service.ts:
getPeople(): Observable<Person[]> {
return this.http.get<Person[]>(People_Names_API)
.toPromise()
.then(names => names ['name']
.forEach(personName=> {
const personDetailUrl = '/person-detail/v2/' + personName;
this.http.get(personDetailUrl )
.toPromise()
.then((res: Person) => {
return res;
});
})
);
The problem is I can't return a single Observable<Person[]> and the view is always getting undefined.
Please help!