Issue
In the service, there is this code :
getUser(id){
return this.http.get('http:..../' + id)
.map(res => res.json());
}
In the component :
this.myService.getUser(this.id).subscribe((customer) => {
console.log(customer);
this.customer = customer,
(err) => console.log(err)
});
When it's the 'customer' exist, no problem I get all the information about the customer.
When the id does not exist, the web api return 'BadRequest' with a message. How can I get this message ? the status ?
Thanks,
Solution
(err)
needs to be outside the customer
fat arrow:
this.myService.getUser(this.id).subscribe((customer) => {
console.log(customer);
this.customer = customer,
},
(err) => {console.log(err)});
To get the error msg back, add a catch
that will return the error object:
getUser(id){
return this.http.get('http:..../' + id)
.map(res => res.json())
.catch(this.handleError);
}
private handleError(error: any) {
let errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
return Observable.throw(error);
}
Answered By - Nehal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.