2016-11-12 150 views
0

我的想法是这样:如何从订阅中返回观察值以订阅?

getCast(id:number, numCast:number){ 
     return this.getCredits(id) 
      .map(credits => credits.cast.length >= numCast ? credits.cast.slice(0,numCast) : credits.cast) 
      .subscribe(character => { 
       for(let index in character){ 
        return this.getPerson(character[index].id); 
       } 
      }); 
    } 

getPerson(person_id:number) { 
    return this.http.get('https://api.themoviedb.org/3/person/'+person_id+'?'+this.apiKey) 
        .map(data => JSON.parse(data['_body'])) 
} 

this.mvs.getCast(this.route.snapshot.params['id'], 6) 
     .subscribe(data => { console.log(data); }) 

,但它并不在所有的工作。 在控制台中的错误说:

_this.mvs.getCast(...).subscribe is not a function 
+0

我不确定你要在这里做什么。如果您已经在映射之后订阅了它,为什么在'this.mvs.getCast'中再次订阅?你能提供一个Plunker吗? – brians69

+0

Beucase getCast应该返回演员的可观察者,为此我需要获得电影的演员阵容,然后我需要每个演员的细节。 我想从** this.getPerson(character [index] .id)返回观察值; ** – Donovant

+0

如果你想在'getCast'之外'订阅',你不应该在'subscribe'里面。使用另一个'map'来代替,所以你仍然返回一个可观察的值。 – jonrsharpe

回答

0

这里的问题是,你订阅两次getCast(内部和外部)。像这样的东西应该工作:

从电影

getCast(id:number, numCast:number) { 
    return this.getCredits(id) 
    .map(credits => credits.cast.length >= numCast ? credits.cast.slice(0,numCast) : credits.cast); 
} 

订阅getCast获得投,并得到所有参与者

this.mvs.getCast(this.route.snapshot.params['id'], 6) 
    .subscribe(character => { 
    for(let index in character) { 
     return this.getPerson(character[index].id); 
    } 
    }) 

然后,订阅getPerson显示的演员的数据

getPerson(person_id: number) { 
    return this.http.get(`https://api.themoviedb.org/3/person/${person_id}?${this.apiKey}`) 
    .map(data => JSON.parse(data['_body'])) 
    .subcribe(actor => this.actor = actor); 
}