Home > database >  Type 'Observable<Observable<Employee[]>>' is not assignable to type 'Obse
Type 'Observable<Observable<Employee[]>>' is not assignable to type 'Obse

Time:03-19

I'm trying to filter a mat-autocomplete form this way:

ngOnInit function:

filterEmployee() {
    this.filteredOptions = this.employee.valueChanges.pipe(
      startWith(""),
      map((value) => this._filter(value))
    );
  }

Filter function

private _filter(value: string): Observable<Employee[]> {
    const filterValue = value && value.toLowerCase();
    return of(
      this.employees.filter(
        (employee) =>
          employee.name.toLowerCase().indexOf(filterValue) > 1
      )
    );
  }

And there's this error:

enter image description here

What am I doing wrong?

CodePudding user response:

You try to return Observable of array of strings but you have write Observable of employees. You can update Observable<string[]> like this :

private _filter(value: string): Observable<string[]> {
    const filterValue = value && value.toLowerCase();
    return of(
      this.employees.filter(
        (employee) =>
          employee.name.toLowerCase().indexOf(filterValue) > 1
      )
    );
  }

CodePudding user response:

first of all, don't map() to an Observable (I mean, you could, but that's not what you want here). I think you've confused map with flatMap.

private _filter(value: string): Employee[] {
  const filterValue = value && value.toLowerCase();
    return employees.filter(
      (employee) =>
        employee.name.toLowerCase().indexOf(filterValue) > 1
    );
}

Now you'll assign Observable<Employee[]> instead of Observable<Observable<Employee[]>>

Can we see the definition for this.employees and Employee?

  • Related