0

My idea was to do this :

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); })

but it doesn't work at all. The error in console says:

_this.mvs.getCast(...).subscribe is not a function
Donovant
  • 3,091
  • 8
  • 40
  • 68
  • I'm not sure what you're trying to do here. Why are subscribing again in `this.mvs.getCast` if you already subscribed it after mapping? Can you provide a Plunker, please? – brians69 Nov 12 '16 at 22:22
  • Beucase getCast should return an observable of actors, for this I need to get the the cast of a movie then I need the details of each actor. I want to return the observable from **this.getPerson(character[index].id);** – Donovant Nov 12 '16 at 22:26
  • If you want to `subscribe` outside of `getCast`, you shouldn't `subscribe` inside. Use another `map` instead, so you still return an observable.. – jonrsharpe Nov 12 '16 at 22:31
  • possibly dublicate of http://stackoverflow.com/questions/39935721/how-to-return-observable-from-subscribe – ishandutta2007 Apr 12 '17 at 16:17

1 Answers1

0

The problem here is that you're subscribing twice to getCast (inside and outside). Something like this should work:

Get the cast from the movie

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

Subscribe to getCast and get all actors

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

Then, subscribe to getPerson to display an actor's data

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);
}
brians69
  • 485
  • 2
  • 11
  • 24