await function on ngOnInit returning undefined

  angular, async-await

I am trying to get specific data before doing other functions as the other functions rely on that function to assign variables but I get an undefined error when refreshing but if I navigate to the place I need to be, It wont throw the undefined error, Here is my code:

public async ngOnInit() {
    const getdata = await this.websiteSettingsService.webConfig;
    this.getId(getdata);
    this.TournamentDetails = this.tournamentService.getMainTournamentDetails(this.leaderboardId).pipe(
      takeUntil(this.unsubscribe),
      shareReplay(1)
    );

    this.TournamentDetails.subscribe((response) => {
      this.getTournamentDetails(response);
    });
    }
  public getTournamentDetails(response: TournamentStatusModel) {
    this.TournamentStatus = response;

  }

  public getId(response: any) {
    response.forEach((ID) => {
      if (ID.settingName.includes('current_tournament')) {
        this.leaderboardId = ID.value;
      }
    });
  }

The error comes in my getId function saying TypeError: can't access property "forEach", response is undefined

But I am awaiting data first so I am confused as to why it is doing this

Source: Angular Questions

One Reply to “await function on ngOnInit returning undefined”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.