Home > Software design >  Unable to filter elements from getRequest
Unable to filter elements from getRequest

Time:08-30

A have found lots of similar topics, too many to list. I have tried them all but I keep getting error. I tried using map, I tried using pipe, I tried using filter, I've tried to combine them in every way possible. So please don't close it for a repost but show me how to solve it. I need to filter a get request and save the filtered list in an array. This is based on attribute status. The code is self explanatory. My last request is the one which I have the least uncompilable code. But it says: "Property 'subscribe' does not exist on type 'Promise'". Whereas it is not about a void promise at all, I use an observable. Converting to Promise also didn't work.

service request:

  getAllTimesheets(): Observable<TimeSheet[]>{
    const url = environment.TIMESHEETSAPI_URL   "timesheets/findAll";
      return this.http.get<TimeSheet[]>(url);
  }

request in component.ts:

  timesheets!:TimeSheet[];

  getConfirmedSheet(): TimeSheet[]{
    return this.timesheetService.getAllTimesheets().forEach(sheets => sheets.filter(sheet => sheet.status == 'CONFIRMED')).subscribe(sheets => this.timesheets = sheets);
  }

CodePudding user response:

You need to subscribe to observable to make http request, in response you can filter anything you like.

getConfirmedSheet(): void {
  return this.timesheetService.getAllTimesheets().subscribe({next: (response: TimeSheet[]) => {
    response.filter(...
  }});
}

CodePudding user response:

Try that : this.timesheetService.getAllTimesheets().pipe(filter(timeSheet => timeSheet.status == 'CONFIRMED' )).subscribe(timeSheets => {this.timesheets = timeSheets});

  • Related