我正在关注英雄之旅教程。而不是getHero()
我有一个getCustomer()
方法。我收到以下错误:
EXCEPTION: Cannot read property '0' of undefined
为什么我收到此错误的任何想法?我认为handleError()
函数可能存在问题。
getCustomer(id: string): void {
this.customerService.getCustomer(id)
.subscribe(
customer => this.customer = customer,
error => this.errorMessage = <any>error
);
}
getCustomer(id: string): Observable<Customer> {
const url = `${this.customerUrl}/${id}`;
return this.http.get(url)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || {};
}
private handleError(error: Response | any) {
// In a real world app, you might use a remote logging infrastructure
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}