Home > OS >  Observable<unknown>
Observable<unknown>

Time:08-13

getOrders(id: number) : Observable<IOrder[]> {
    return this.http.get<IOrder[]>(this.baseUrl   'orders.json')
      .pipe(
        map(orders => {
          let custOrders = orders.filter((order: IOrder) => order.customerId === id);
          return custOrders;
        }),
        catchError(this.handleError)
      );
  }

I get this error:

Type 'Observable' is not assignable to type 'Observable<IOrder[]>'. Type 'unknown' is not assignable to type 'IOrder[]'.ts(2322)

How do I solve this error?

CodePudding user response:

Well, it depends on what your handleError is actually doing, but error means that you expect it to return observable of IOrder[], and you don't.

Use getOrders(id: number) : Observable<unknown> { (look at official example https://stackblitz.com/run?file=src/app/heroes/heroes.service.ts) or change your handleError to return correct value.

CodePudding user response:

The api also returns an observable so it should be like this.

getOrders(id: number) : Observable<IOrder[]> {
    return this.http.get<Observable<IOrder[]>>(this.baseUrl   'orders.json')
      .pipe(
        map(orders => {
          let custOrders = orders.filter((order: IOrder) => order.customerId === id);
          return custOrders;
        }),
        catchError(this.handleError)
      );
  }
  • Related