Home > Back-end >  How I can sort my data with sort() in Angular 12? [closed]
How I can sort my data with sort() in Angular 12? [closed]

Time:09-21

I get my data from my API, I have already made my call and I display it.

I would like to order it alphabetically.

I don't understand why my console say : 'ERROR TypeError: Cannot read properties of undefined (reading 'sort')'

enter image description here

CodePudding user response:

Because you are getting the clubs in one async operation and you launch sort operation immediately and the data didn't arrive yet. You must chain the operation:

constructor(...) {
  this.getClubs().subscribe(clubs => {
    this.clubs = clubs;
    this.sortClubs();
  });
}

private getClubs() {
  return this.httpClient.get('...')
}

  • Related