I'm encountering two issues while attempting to display data from my API call using the following code...
API Call:
getProducts(id: number) {
return from(Preferences.get({ key: 'TOKEN_KEY' })).pipe(
switchMap(token => {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token.value}`);
return this.httpClient.get(`${environment.apiUrl}products?category=${id}`, { headers, observe: 'response' });
}),
catchError(err => {
console.log(err.status);
if (err.status === 400) {
console.log(err.error.message);
}
if (err.status === 401) {
this.authService.logout();
this.router.navigateByUrl('/login', { replaceUrl: true });
}
return EMPTY;
}),
map(res => res.body)
);
}
Your Code:
export class SubcategoryPage implements OnInit {
subcategory: any = [];
products: any = [];
constructor(
private route: ActivatedRoute,
private categoriesService: CategoriesService,
) { }
ngOnInit() {
this.getSubCategory();
this.getProducts();
}
// More methods and functions...
Browser Console Error:
Cannot read properties of undefined (reading 'map')
Sample API Response:
{
// Sample API response for reference
}
What could be causing the error in your code?