Home > Blockchain >  Type 'IProductsAvailable' is missing the following properties from type 'IProductsAva
Type 'IProductsAvailable' is missing the following properties from type 'IProductsAva

Time:03-14

Can anyone help me with the following error when trying to use an interface in my application

Type 'IProductsAvailable' is missing the following properties from type 'IProductsAvailable[]': length, pop, push, concat, and 28 more

Service

getProducts() {
  return this.http.get<IProductsAvailable>("https://fakestoreapi.com/products")
}

Interface

export interface IProductsAvailable {
    id: string;
    title: string;
    category: string;
    description: number;
    image: string;
    price: number;
    rating: []
}  

Component

 productsAvailable:IProductsAvailable[];

 this._productsService.getProducts().subscribe((res)=> {
   console.log(res);
   this.productsAvailable = res;
 });

When I try to assign the response from the service to the productsAvailable variable I get the error.

CodePudding user response:

In your service, you should specify that the generic type should be an array.

getProducts() {
  return this.http.get<IProductsAvailable[]>("https://fakestoreapi.com/products")
}

CodePudding user response:

Change service to this :

getProducts(): Observable<Array<IProductsAvailable>> {

  return this.http.get<Array<IProductsAvailable>>
      ("https://fakestoreapi.com/products");

}
  • Related