My current challenge involves hitting one API to retrieve a specific string, storing it in a variable, and then passing it to another HTTP API call. However, I'm encountering an issue where the API call requiring the argument executes but never sends the correct request. (Just a beginner here by the way)
Below are the two API calls, with some personal information removed for privacy.
async getMatches() {
return this.http.get('matchesUrl', {
headers: {
'Authorization': 'Bearer **'
}
})
}
async getMatchStats(matchId: string) {
return this.http.get(`specificMatchUrl/${matchId}/`, {
headers: {
'Authorization': 'Bearer **'
}
})
}
And here is my component:
import { Component, OnInit } from '@angular/core';
import {GetapiService} from '../getapi.service';
@Component({
selector: 'app-gamingpage',
templateUrl: './gamingpage.component.html',
styleUrls: ['./gamingpage.component.scss']
})
export class GamingpageComponent implements OnInit {
title = '';
wins = '';
totalMatches = '';
currentWinStreak = '';
latestMap = '';
matchId = '';
constructor(
private api: GetapiService
) { }
async ngOnInit() {
this.api.getPlayerStats().subscribe((data: any) => {
this.title = data['game_id'];
this.wins = data.lifetime['Wins']
this.totalMatches = data.lifetime['Matches']
this.currentWinStreak = data.lifetime['Current Win Streak']
});
(await this.api.getMatches()).subscribe((data: any) => {
this.matchId = data.items[0].match_id;
});
(await this.api.getMatchStats(this.matchId)).subscribe((data: any) => {
this.latestMap = data.rounds.round_stats['Map']
})
}
}
I initially believed that awaiting both calls would resolve this issue because getMatchStats won't be triggered until getMatches is completed and the matchId variable is assigned. However, upon making the call, the URL generated is incorrect and does not include the matchId. Despite console.log displaying the value correctly, there seems to be a misunderstanding on my end. I've checked similar questions but none seem to address my specific scenario... or at least as far as I comprehend. Thank you in advance for your assistance.