Property 'role' does not exist on type 'Users[]'

I have a database of users, with a property "role" that can be either a driver or an admin, in my driver.service, I am trying to getUsers and filter them by role, in other words, I just want to get the list of users with the role driver but when I write my code it says that the property role does not exist on type Users[] although I added it in the class users.ts this is my class:

export class Users {
    id: number;
    CIN: number;
    name: string;
    address: string;
    email: string;
    username: string;
    password: string;
    permis: string;
    prenom: string;
    image: string; 
    modeAuto: number;
    sex: string;
    dateNais: Date;
    role: string;
}

This is my service with the Getusers method i tried to modify to use the filter :

 public urls = 'http://127.0.0.1:8000/api/v1/users'; // url pour la récupération de la partie backend(laravel)

  constructor(private http: HttpClient) { }
  //* afficher tous les conducteurs*/

  getUsers (): Observable<Users[]> {
    return this.http.get<Users[]>(this.urls).filter(x => x.role === 'driver').pipe(
      tap(_ => console.log('fetched Users')),
      catchError(this.handleError<Users[]>('getUsers', []))
    );
  }

Thanks for the help

1 answer

  • answered 2021-05-14 17:59 Amit Bisht

    You have placed your filter condition incorrectly, you cannot use any rxjs operator directly on the Observable type

    getUsers (): Observable<Users[]> {
        return this.http.get<Users[]>(this.urls).pipe(
          tap(_ => console.log('fetched Users')),
          map((m) => m.filter(x => x.role === 'driver')),
          catchError(this.handleError<Users[]>('getUsers', []))
        );
      }