I'm new to angular 2 and would like to make nested http calls where one call is dependent on the next call. My code is as follows in service:
getUserPosts(): Observable<any[]>{
return this.http.get(this.apiUserURL + this.userId + '/')
.map((response: Response) => response.json())
.flatMap((users) => {
return Observable.forkJoin(
users.map(user=>
this.http.get(this.apiUserPostURL + user.id)
.flatMap(response => response.json())
)
)
})
}
in Components I have:
ngOnInit() {
this.results = this.postsService.getUserPosts()
.subscribe(posts => { this.posts = posts
console.log(posts);
})
}
when I do that I get the last post of every user but not all posts. (I have 3 posts for every user, and I have two users in my database, but below I get only one post of every user:
(2) [Object, Object]
0:Object
1:Object
solution expected: get all posts from all users like this:
(6) [Object, Object, Object, Object, Object, Object]
when I change the last part of my service to the following:
users.map(user=>
this.http.get(this.apiUserPostURL + user.id)
.map(response => response.json())
)
I get two arrays of 3 posts each and I don't know how to access those posts with *ngFor:
(2) [Array(3), Array(3)]
0:Array(3)
0:Object
1:Object
2:Object
1:Array(3)
0:Object
1:Object
2:Object
Solution expected: reach out directly to the arrays without having one array of objects for every user but instead one array containing all objects:
(6) [Object, Object, Object, Object, Object, Object]
I even tried to do something else which I thought will help by adding .flatMap to my component as follows:
ngOnInit() {
this.results = this.postsService.getUserPosts()
.flatMap(posts => this.posts = posts)
.subscribe(posts => { this.posts = posts
console.log(posts);
})
}
but I got two separate arrays, each array is related to each user with all his posts, *ngFor only picks the latest user and ignore the other posts from the other user:
(3) [Object, Object, Object]
0: Object
1: Object
2: Object
(3) [Object, Object, Object]
0: Object
1: Object
2: Object
Solution expected: merge both arrays into one array as:
(6) [Object, Object, Object, Object, Object, Object]
Can anyone please help in figuring out how to solve this problem? Thanks a lot!