2016-11-12 152 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); 
}