简体   繁体   English

在angular2 rxjs中嵌套HTTP调用

[英]nested http calls in angular2 rxjs

I'm new to angular 2 and would like to make nested http calls where one call is dependent on the next call. 我是angular 2的新手,我想进行嵌套的http调用,其中一个调用取决于下一个调用。 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: (我为每个用户发布了3个帖子,并且我的数据库中有两个用户,但是在下面,我仅获得每个用户的一个帖子:

(2) [Object, Object] 0:Object 1:Object (2)[对象,对象] 0:对象1:对象

solution expected: get all posts from all users like this: (6) [Object, Object, Object, Object, Object, Object] 预期的解决方案:像这样从所有用户那里获取所有帖子:(6)[对象,对象,对象,对象,对象,对象]

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: 我得到两个数组,每个数组包含3个帖子,我不知道如何使用* ngFor访问这些帖子:

(2) [Array(3), Array(3)] 0:Array(3) 0:Object 1:Object 2:Object 1:Array(3) 0:Object 1:Object 2:Object (2)[Array(3),Array(3)] 0:Array(3)0:对象1:对象2:对象1:Array(3)0:对象1:对象2:对象

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] 预期的解决方案:直接与阵列建立联系,而不必为每个用户拥有一个对象阵列,而是一个包含所有对象的阵列:(6)[Object,Object,Object,Object,Object,Object]

I even tried to do something else wich i thought will help by adding .flatMap to my component as follows: 我什至尝试做其他事情,我认为可以通过将.flatMap添加到我的组件中来帮助如下:

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: 但是我有两个单独的数组,每个数组都与每个用户及其所有帖子相关,* ngFor仅选择最新用户,而忽略其他用户的其他帖子:

(3) [Object, Object, Object] 0: Object 1: Object 2: Object (3) [Object, Object, Object] 0: Object 1: Object 2: Object (3)[对象,对象,对象] 0:对象1:对象2:对象(3)[对象,对象,对象] 0:对象1:对象2:对象

Solution expected: merge both arrays into one array as: (6) [Object, Object, Object, Object, Object, Object] 预期的解决方案:将两个数组合并为一个数组,如下所示:(6)[对象,对象,对象,对象,对象,对象]

Can anyone please help in figuring out how to solve this problem? 谁能帮忙弄清楚如何解决这个问题? Thanks a lot!! 非常感谢!!

Thank you to Julia Passynkova for her answer to allow to solve my problem. 感谢Julia Passynkova的回答,以解决我的问题。 this the final solution by adding the last line of concat.apply() to my service: 通过将concat.apply()的最后一行添加到我的服务中,这是最终解决方案:

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())   
            )   
        )
    }).map(x=> [].concat.apply([],x))

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM