I am attempting to retrieve JSON from a 404 response, but I am only receiving the
Response {_body: "{myJSON}", status: 404, ok: false, statusText: "Not Found", headers: Headers…}
How can I access the object
itself so that I can display it in my HTML using something like {{error.text}}
, similar to how I did with http.get()
?
Below is my code :
getList(): Promise<Response> {
return this.http.get(this.apiUrl+'list/')
.toPromise()
.then(response => {
console.log(response.json()); return response.json()})
.catch(this.handleErrorPromise)
}
//if an error ocurred while doing api calls
private handleErrorPromise (error: Response | any) {
console.error("EEEEE" + error.message || error);
return Promise.reject(error.message || error);
}
When it reaches the .then
part, everything works fine and it returns an object which I can use in my component
(I'm currently implementing it this way... please let me know if there's a better approach)
ngOnInit(): void {
this.ListServiceProvider.getList().then(
list=> {this.mList= list; console.log(list)},
error => {this.errorMessage = error; console.log(error)});
}
In my HTML, I have a *ngIf=errorMessage
set up to show when there is an error, however, it doesn't display the JSON as expected. Can you help me identify what may be wrong with my code?