所以我遵循了角度文档中的错误处理指南。我似乎总是在我的服务中收到错误,但是,我的控制台总是将我的错误对象显示为'[object Object]'。有谁知道是什么导致这种情况以及如何解决这个问题?
我的服务文件
subViewPlus(section, sign){
return this.http.get('http://localhost:3000/forum/subViewPlus/' + section + '/' + sign)
.pipe(catchError(this.handleError)
}
private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.error(
`Backend returned code ${error.status}, ` +
`body was: ${error.error}`);
}
// return an ErrorObservable with a user-facing error message
return new ErrorObservable(
'Something bad happened; please try again later.');
};
显示控制台日志的屏幕截图:如何将此[正文]转换为更有用的内容?
答案 0 :(得分:1)
不要对对象进行插值。而是做这样的事情。
console.error({
"Backend returned code: ": error.status,
"body was: ": error.error
})