So, I have server which on request returns array of objects
Example:
[
{
id: 1
name: name1
},
{
id: 2
name: name2
}
]
Array can have infinite elements;
After that I have to send new requests for each element in array with id of response.
example:
https://localhost/api/parent/${id}/child
response:
[
{
childId: 1
name: name1
},
{
childId: 2
name: name2
},
{
childId: 3
name: name3
}
]
And after that I have to send new requests for each response of new requests
example:
https://localhost/api/parent/${id}/child/${childId}/grandChild
response:
[
{
grandChildId: 1
name: name1
},
{
grandChildId: 2
name: name2
},
{
grandChildId: 3
name: name3
}
]
and response is similiar like others
and after that i have to organize data to look like this
[
{
id: 1
name: name1
children: [
{
childId: 1
name: name1,
grandChildren: [
{
grandChildId: 1
name: name1
},
{
grandChildId: 2
name: name2
},
{
grandChildId: 3
name: name3
}
]
},
{
childId: 2
name: name2
},
{
childId: 3
name: name3
}
]
},
{
id: 2
name: name2
}
]
Each parent has array of children and each child has array of grandChildren.
This was my try but if i try in component to do parents.forEach(parent => console.log(parent.childern)) it is undefined
getData() {
return this.http.get<Parent[]>(baseUrl + 'parent', {withCredentials: true}).pipe(
map(parents => {
parents.forEach(parent => {
this.getChildren(parent).subscribe(Children => {
parent.Children = Children;
parent.Children.forEach(Children => {
this.getGrandChildren(Children).subscribe(grandChild => {
Children.grandChildren = grandChild;
});
});
});
});
return parents;
})
);
}
getChildren(parent: Parent): Observable<Child[]> {
return this.http.get<Children[]>(baseUrl + `parent/${parent.id}/children`, {withCredentials: true});
}
getGrandChildren(child: Child): Observable<GrandChild[]> {
return this.http.get<GrandChild[]>(baseUrl + `children/${child.id}/GrandChildren`, {withCredentials: true});
}