Home > other >  Angular 10 : Remove this use of the output from "getTableStat"; "getTableStat" d
Angular 10 : Remove this use of the output from "getTableStat"; "getTableStat" d

Time:03-21

i get this message error :Remove this use of the output from "getTableStat"; "getTableStat" doesn't return anything

loadPage() {
const tableStat: any = this.getTableStat();
this.sortColumn = tableStat?.sortColumn;
this.sortDirection = tableStat?.sortDirection;
this.selection.clear();
}

private getTableStat(): void {
return isDefined(this.tableStat)
  ? this.tableService.getStat(this.tableStat)
  : null;
}

please help me to resolve the issue.

CodePudding user response:

If you are expecting it to return tableStat (assuming it is defined??) then the return Type should be whatever type that tableStat is (preferably NOT < any >).

The following sets an interface for the expected structure of the TableStat type (I just made it up based on the two variables you mentioned - not knowing what the full structure actually is) and then uses that as the return type from the method that gets the tableStat.

also - not knowing the code - but unless isDefined(...) is in the global space - you may need it to be this.isDefined(...) which can also be a source of the data being undefined.

export interface TableStat {
  sortColumn: string;
  sortDirection: string;
}

... 

private getTableStat(): TableStat{
return isDefined(this.tableStat)
  ? this.tableService.getStat(this.tableStat)
  : null;
}

CodePudding user response:

You have bad code in more lines there:

-1) you said to your method getTableStat to return nothing (declaring the return type as void), but inside your method you return a null value or call another function this.tableService.getStat(this.tableStat) which probably will return some data. So, I'm almost sure you need to change the return type of your getTableStat method;

-2) inside loadPage you use getTableStat response to declare a constant, but due of 1st step (your method returning nothing), you get the error;

Even if is not recommandating, you can declare the type to any instead of void and probably will solve this issue (or just remove the void type and the compiler will detect automatically the data):

private getTableStat(): any {
   //
}

If your service getStats returns an Observable, you need to do some more changes there because you'll need to subscribe to it

How I'll do this:

loadPage(): void {
  this.getTableStat().subscribe(stats => {
    if (typeof stats === 'undefined') {
      return;
    }

    this.sortColumn = stats.sortColumn;
    this.sortDirection = stats.sortDirection;
    this.selection.clear();
  });
}

private getTableStat(): Observable<yourDataType | undefined> {
  return isDefined(this.tableStat)
    ? this.tableService.getStat(this.tableStat)
    : of(undefined); // of is an RxJS operator
}
  • Related