I have a rest endpoint /Products/latest
which returns 204 in case there are no products, and the following generic angular service:
getFromAPI(url: string) {
const params = new HttpParams();
return this.http
.get(url, { params: params })
.catch((err: HttpErrorResponse) => {
console.error(`Backend returned code ${err.status}, body was: ${err.error}`);
return Observable.of([]);
});
}
and
getFromAPI() {
return this.masterService.get('/Products/latest').map((res: Response) => {
if (res.status === 204) {
return Observable.of([]);
} else {
return res;
}
});
}
However, when the servies results a 204 code, I'm getting the following error:
TypeError: Cannot read property 'status' of null
How can this be happening? Why is the whole response null if the API responds with a 204?