What is the best way to send an observable back from a subscribe function to set up another subscription?

After brainstorming, I came up with this approach:

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

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

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

However, this implementation is not functioning as expected. The error displayed in the console reads:

_this.mvs.fetchCastDetails(...).subscribe is not a function

Answer №1

One issue is that you are subscribing twice to the getCast function, both inside and outside. To resolve this, consider the following approach:

Retrieve the cast information for 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 to fetch all actors

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

Next, subscribe to getPerson to display detailed actor information

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered while utilizing a custom third-party component within an Angular project during static analysis

Currently, I am utilizing an Angular (2+) datepicker component (link to Github) in two separate Angular projects: Angular CLI v1.0.0-beta.30, Angular v2.3 Angular CLI v1.0.0, Angular v4.0 The first project works flawlessly both during development with n ...

What are the reasons behind the issues encountered when enabling "Optimization" feature in Angular that affect the proper rendering of PrimeNg elements?

Angular Version : 9.x Primeng Version : 9.x We've encountered an issue with PrimeNg elements not rendering correctly in our dev/prod environments, although they work fine in the local environment. After some investigation, we found that the culprit ...

Angular: understanding the intricacies of initializing variables within the *ngFor directive

I'm working with an Angular template that looks like this: <li *ngFor="let item of list | keyvalue" let-rand="random()"> <input type="radio" class="form-check-input" id="radio-{{rand}}" name ...

RxJS: when combined with timer, groupBy operator does not emit any values

Just starting out with RxJS version 6.5.5, I'm encountering an issue with the groupBy operator. Here's a simplified example to showcase the problem. I have a function called retrieveFiles() that retrieves an array of strings. function async ret ...

Display JSON information in a tabular format

I am trying to populate a table with data from an API response, but I am facing issues printing the data. Specifically, I need to match the "IdLangage" value with the correct cell in the table and display the corresponding translation. The JSON data format ...

What is the process for assigning custom constructor parameters to an Angular Service during its creation in an Angular Component?

I have been tasked with converting a Typescript class into an Angular 6 service: export class TestClass { customParam1; customParam2; constructor(customParam1, custom1Param2) { this.customParam1 = customParam1; this.customPara ...

The localhost HTTPClient throws an unrecognized error, despite the fact that the URL can be reached via the browser and has CORS enabled

I'm still struggling with this issue. Despite having a functioning Angular-Express app on the cloud, I encounter errors when trying to run it locally. It appears that the requests do not reach the backend at all, even though entering the URL into a b ...

Writing Data to Google Cloud Firestore Map using NextJS and Typescript with nested objects

I could use some assistance. I'm developing an application using NextJS (React), TypeScript, and Google Cloud Firestore. Everything seems to be working fine so far. However, I'm facing an issue with storing the address and phone number in a neste ...

Utilize a singular loader when simultaneously making numerous HTTP requests in Angular with ngx-ui-loader

After successfully implementing the ngx-ui-loader and setting the NgxUiLoaderHttpModule for all HTTP requests in my project, everything is working fine. However, I have encountered an issue on certain routes where multiple HTTP requests are being executed ...

Using Angular 5 to integrate a jQuery plugin

Recently, I've started learning Angular and am currently using version 5. I need to integrate a plugin called 'jquery-circle-progress' into my project. The plugin can be found at this link: I managed to install the plugin using npm and adde ...

Utilizing TypeORM to selectively choose data in OneToMany relationships

I am looking to create a TypeORM query that pulls data from the database. Specifically, I want to retrieve all clients who have made a purchase but have not initiated a return. Here is the structure of the database: Clients: Id (Int, primary column) Purc ...

Is there a way in NodeJS to preview the contents of a file in a browser before initiating the download process?

Is there a way to preview a file in the browser before downloading it in NodeJS? This would allow users to make sure they are choosing the correct file to download. Currently, I have this code for downloading a file: app.get("/download/file", (req, res) = ...

Different States for a single element within the React + Redux Template in Visual Studio

I have come across an issue while using the Visual Studio 2017 React + Redux template. I followed the setup for stores as per their guidelines and everything was working fine so far. However, now I need to provide a component access to multiple states. The ...

Personalized prefix for Angular and WebStorm components

After starting a project in Angular with Visual Studio Code a few months ago, I decided to switch to WebStorm and upgraded the project to Angular 4.0 today. Although my project is running smoothly without any errors, I encountered an issue in WebStorm: ...

What is the process for defining the type or interface of an object in Visual Studio Code?

Is there a way to create a new type or interface by replicating the structure of a complex object that is imported from a library? For instance, in the image below, the object Text is taken from react-three/drei. https://i.sstatic.net/BcUzd.png Upon inspe ...

Having trouble converting customized tabs into Bootstrap navigation tabs?

Creating custom tabs with unique tab logic <!-- <lntds-tabs [selectedIndex]="selectedTabIndex" (selectedTabChange)="tabChanged($event)"> --> <!-- <lntds-tab class="custom-tab-group lntdstabsco ...

Avoid connecting redux to a component in TypeScript and React

I am having an issue with the "connect" function not wrapping my App component, causing Redux to not work. I have tried cloning some repositories with react+redux+typescript and they all work fine, but my application does not. As a result, I am unable to ...

Encountering issues when launching Node.js application using PM2 and ts-node in production mode

I've run into an issue while trying to use PM2 with ts-node. Whenever I attempt to start my application using PM2, I receive an error message stating that the ts-node interpreter is not found in the PATH. I've experimented with installing ts-nod ...

Visual Studio - Error TS1005 'Unexpected token'

After spending nearly 5 hours scouring the internet for a solution, I am still unable to resolve this persistent issue. The responses I've found so far do not seem to address the specific problem I'm facing. Although I have upgraded the tsc vers ...

An issue has occurred while attempting to differentiate '[object Object]'. Please note that only arrays and iterable objects are permitted

myComponent.component.ts ngOnInit() { this.getData.getAllData().subscribe( response => { console.log(response); this.dataArray = response; }, () => console.log('there was an error') ); } myservi ...