Recently, I embarked on my journey to learn Angular. While perusing a guide on YouTube, I encountered an error while trying to replicate the code.
Here's my objective:
- I aim to fetch data from fakestoreapi.com/products that aligns with my defined interface IProduct.
- The fetched elements should total 5 in number.
- A delay of 2 seconds needs to be incorporated.
This is the snippet of code I have implemented so far:
import { Injectable } from "@angular/core";
import {HttpClient, HttpErrorResponse, HttpParams} from "@angular/common/http";
import {catchError, delay, Observable} from "rxjs";
import {IProduct} from "../models/product";
@Injectable({
providedIn: 'root'
})
export class ProductsService {
constructor(private http: HttpClient) {
}
getAll(): Observable<IProduct[]> {
return this.http.get<IProduct[]>("https://fakestoreapi.com/products", {
params: new HttpParams({
fromObject: {limit: 5}
}).pipe( //error
delay(2000)
)
});
}
}
Surprisingly, the method utilized in this code was not found documented, but rather featured in the guide I followed!
Take a look at a screenshot from the guide for reference: enter image description here