I am currently developing an application with a Python backend and Angular frontend. The main functionality of the app involves retrieving FIFA players from a MongoDB using the getLoyalPlayers
function.
Below is the snippet from the loyalty.component.ts
file:
import { Component } from "@angular/core";
import { WebService } from './web.service';
@Component({
selector: 'loyal',
templateUrl: './loyal.component.html',
styleUrls: ['./loyal.component.css']
})
export class LoyalComponent {
constructor(public webService: WebService) {}
ngOnInit() {
this.player_list = this.webService.getLoyalPlayers();
}
player_list: any = [];
}
This is the section from web.service.ts
:
getLoyalPlayers(loyalPage: number) {
return this.http.get('http://localhost:5000/api/v1.0/loyal-players');
}
The retrieved data is then rendered in loyalty.component.html
using async
:
<div *ngFor="let player of player_list | async">
<!-- Individual items can be accessed like -->
{{ player.short_name }}
</div>
In trying to access the elements within this array in the typescript file, I attempted the following approach:
ngOnInit() {
if (sessionStorage['loyalPage']) {
this.page = Number(sessionStorage['loyalPage']);
}
this.player_list = this.webService.getLoyalPlayers(this.page);
console.log(this.player_list.short_name)
}
However, this resulted in a value of undefined
:
https://i.sstatic.net/wvRnh.png
I am seeking guidance on how to properly retrieve this data. How should I proceed?