Show information retrieved from one API request within another API request

Currently, I am in the process of retrieving data from the Youtube API by utilizing 2 separate requests. One request is used to fetch a list of videos, while the other request provides details for each individual video.

The initial request successfully displays 4 videos with their corresponding thumbnails and titles. In an attempt to gather more information for each video, I experimented with implementing a foreach loop inside my primary API call:

Below is an excerpt from my service.ts file:

    export class YoutubeDataService {

      constructor(private http: HttpClient) { }

      getList() {
         return this.http.get('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCiRDO4sVx9dsyMm9F7eWMvw&order=date&maxResults=4&type=video&key={MY_KEY}')
      }

      getViews(id) {
         return this.http.get('https://www.googleapis.com/youtube/v3/videos?part=statistics&id=' + id + '&key={MY_KEY}');
      }
   }

Next, we have my component.ts:

    export class VideosComponent implements OnInit {

      videos: Object;
      items = [];
      views: Object;

      constructor(private youtube: YoutubeDataService) { }

      ngOnInit() {
         this.youtube.getList().subscribe(data => {
            this.videos = data.items;
            console.log(this.videos);
            this.videos.forEach(element => {
               this.youtube.getViews(element.id.videoId).subscribe(data2 => {
                  this.views = data2.items[0].statistics.viewCount;
                  console.log(this.views);
               });
            });
         });
      }

    }

Lastly, let's take a look at my component.html:

    <div class="video col-xl-5" *ngFor="let video of videos.items">
       <a class="row" href="https://www.youtube.com/watch?v={{video.id.videoId}}">
          <img [src]="video.snippet.thumbnails.medium.url">
          <div class="col"> 
             <h3 class="title">{{ video.snippet.title }}</h3>
             // Information that requires fetching from second API call
             <p class="description">{{ video.snippet.description }}</p>
          </div> 
       </a>
    </div>

The code effectively shows the title, thumbnail, and description as intended. Additionally, the console.log(this.views); accurately displays the view count for each video. However, I am currently facing challenges in managing this data.

UPDATE

Upon further review, it became clear that simply pushing the data into an array would resolve the issue when displaying it within the html using an index: component.ts

    this.youtube.getList().subscribe(data => {
       this.videos = data.items;
       this.videos.forEach(element => {
          this.youtube.getViews(element.id.videoId).subscribe(data2 => {
             this.array.push(data2.items[0].statistics.viewCount);
          });
       });
    });

However, a new problem arose where the view counts were not ordered according to the videos. Upon each page refresh, the view counts appeared in a different order each time. Is there a possible solution to address this issue?

Answer №1

Currently executing your initial HTTP call "getList"... utilizing forEach to loop through the results and retrieve the view counts... everything is functioning smoothly.

As you proceed with the second HTTP call "getViews(id)", remember to save the outcomes linked to the VideoId; This simplifies using *ngIf to showcase the results once they are available...

XHTML

<div *ngFor="let video of videos">
  <p> ID# {{video.videoId}}: {{video.title}} "{{video.description}}"</p>
  <div *ngFor="let viewership of views">
    <span *ngIf="viewership.videoId == video.videoId"> number of views: {{viewership.viewCount}} </span>
  </div>
</div>

corresponding TS:

this.videos.forEach(element =>{
  this.views.push({ "videoId":element.videoId, "viewCount": this.getViews(element.videoId) });
});

To see a live demo, visit this link - Enjoy Learning !!

Answer №2

It seems like you're looking to retrieve a response from the first request and pass it on to the second response. This can be achieved using the request module in an easy way. Here's an example:

fetchRequest(){

  request(`FIRST_URL`,function (error, response, body){

  //handle error
  console.log('first request error:', error);

  var firstRes = JSON.parse(body)

     request(`SECOND_URL_WITH_FIRST_RES__${firstRes}`,function (error, response, body){

     //handle error
     console.log('Second request error:', error);

     var secondRes= JSON.parse(body)

     return secondRes
     }
  }

}

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

Are all components in Next.js considered client components by default?

I have created a Next.js app using the app folder and integrated the Next Auth library. To ensure that each page has access to the session, I decided to wrap the entire application in a SessionProvider. However, this led to the necessity of adding the &apo ...

Different tsconfigs assigned to various directories

In my project, I am using Next.js with TypeScript and Cypress for E2E tests. The challenge I am facing is configuring tsc to handle multiple configs for different folders. The tsconfig.json file in the project root for Next.js looks like this: { "c ...

Looking to incorporate Functional Components in React using the package "@types/react" version "^18.0.17"? Learn how here!

With the removal of the children prop from React.FC type, what is the new approach for typing components? ...

Revising input value post model binding

In my scenario, I have a text input that is bound to a model property of type Date: <input type="text" [(ngModel)]="model.DateStart" ngControl="dateStart" id="dateStart" #dateStart /> The value of model.DateStart (which is of type DateTime) looks l ...

TypeScript Interfaces: A Guide to Defining and

In my angular2 application, I have created interfaces for various components. One of these interfaces is specifically for products that are fetched from a remote API. Here is the interface: export interface Product { id: number; name: string; } ...

Looking to include a badge within the nebular menu

Can someone assist me with adding a badge to the Nebular menu to display the inbox count dynamically? Any help would be greatly appreciated. Thanks! import { NbMenuItem } from '@nebular/theme'; export const MENU_ITEMS: NbMenuItem[] = [ { ti ...

Utilizing Typescript for manipulation of Javascript objects

Currently, I am working on a project using Node.js. Within one of my JavaScript files, I have the following object: function Person { this.name = 'Peter', this.lastname = 'Cesar', this.age = 23 } I am trying to create an instanc ...

Arrange the columns in Angular Material Table in various directions

Is there a way to sort all columns in an Angular material table by descending order, while keeping the active column sorted in ascending order? I have been trying to achieve this using the code below: @ViewChild(MatSort) sort: MatSort; <table matSort ...

What could be causing my function to not provide the expected output?

Whenever I try to invoke the function from another part of the code, I encounter an issue where it returns undefined before actually executing the function. The function is stored in a service. login.page.ts: ngOnInit(){ console.log(this.auth.getRole()) ...

Typescript: Assigning Variables Without Prior Declaration

In my upcoming Node.js application, I decided to use TypeScript for development. However, I encountered a perplexing issue while working on the code below: class AuthService { public async init(req: Request, res: Response) { let user: IUser | ...

What are some ways to control providers in targeted tests using ng-mocks?

I recently started utilizing ng-mocks to streamline my testing process. However, I am struggling to figure out how to modify the value of mock providers in nested describes/tests after MockBuilder/MockRender have already been defined. Specifically, my que ...

agm-info-window - Adjusting position to the right

Currently, I am using angular google maps to display maps along with markers and info windows. The issue I am facing is that the info window always appears on top. Is there a way to change its position to the right instead? <agm-map [latitude]="lat" ...

Modify associated dropdown menus

I am trying to create an edit form that includes dependent select fields (such as country, state, city). The issue I am facing is that the edit only works when I reselect the first option (car brand) because I am using the event (change) with $event. How c ...

Is it possible to use an Enum as a type in TypeScript?

Previously, I utilized an enum as a type because the code below is valid: enum Test { A, B, } let a: Test = Test.A However, when using it as the type for React state, my IDE displays an error: Type FetchState is not assignable to type SetStateActi ...

Angular2 combined with redux fails to produce any outcomes

Currently, I am implementing redux in conjunction with angular2 and attempting to make a call to Windows Azure Search. Below is the snippet of code that I have written: types.ts export interface IAppState { languageState?: LanguageState; searchState? ...

Is there a way to prevent prettier from automatically adding a new line when formatting HTML tags with ">"?

While navigating through the Prettier extension in Vscode, I am struggling to find a way to disable a specific scenario. In particular, I am having trouble with the formatting of an html tag. Below is a snippet of code that requires some adjustments whene ...

A novel way to enhance a class: a decorator that incorporates the “identify” class method, enabling the retrieval

I have been given the task to implement a class decorator that adds an "identify" class method. This method should return the class name along with the information passed in the decorator. Let me provide you with an example: typescript @identity(' ...

Transformation occurs once you subscribe to an observable entity

Within the x.component.ts, I initiate the getSomething() method from y.service.ts. Since this method returns an observable, I subscribe to it. However, I encounter a peculiar issue where an object with 4 elements, one being an array of number arrays (numbe ...

Unable to access material datepicker by its element id within ngAfterViewInit function

Upon component loading, I am attempting to open the material DatePicker using its element ID (#). However, when I use ngAfterViewInit, it consistently returns an error stating 'Cannot use 'open' on undefined'. Interestingly, the DatePic ...

Types with conditions but no common parameter

I am looking to define my props as either type A or B. For instance export default function App() { type Checkbox = { type: "checkbox"; checked: boolean; }; type Dropdown = { type: "dropdown"; options: Array<an ...