I have rest api which is returning me json array with data. On client side I use this code to create objects from request.
public getNews(): Observable<News[]> {
return this.http.get(this.baseUrl + '/news')
.pipe(
catchError(this.handleError('getNews', []))
)
.map(res => {
let response: any = res;
return response.map((item) => new News(item));
});
}
Subscribing:
this.restProvider.getNews().subscribe((news: News[]) => {
this.news = news;
});
But how can I resolve issue when response is not an array but only single element? Or response is wrong like for example unexpected data from API?
Currently in those situations my application is throwing this error:
response.map is not a function