4
API

add(data) {
 return this.httpClient.post<any>('/api/v3/templates', data);
}

Observable

this.templateService.add(obj)

.subscribe(
 (response) => { 
  console.log(reposne)
 },
(error) => {
 console.log(error)
 }
);

我的 Post API 以某些消息作为响应返回错误,因为名称已存在但我无法在错误对象中得到该错误对象,该对象打印到控制台

The object which is printed is
{
    error:null
    headers:HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, 
    lazyInit: ƒ}
    message:"Http failure response for http://localhost/api/v3/templates: 
400 Bad Request"
    name:"HttpErrorResponse"
    ok:false
    status:400
    statusText:"Bad Request"
    url:"http://localhost/api/v3/templates"

}

由于我的错误对象没有该响应正文,我如何获得我收到的响应消息。

4

3 回答 3

1

返回的错误通常是 HttpErrorResponse 的实例,其error属性包含包装错误(客户端或服务器)

https://angular.io/guide/http#getting-error-details

所以通常你应该能够处理这样的错误。

add(data) {
 return this.httpClient.post<any>('/api/v3/templates', data)
  .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.');
};

如果该error属性为 null,这可能是因为服务器没有返回任何内容。您是否使用邮递员或其他工具检查您的服务器是否返回了适当的响应?

于 2018-04-08T07:45:32.960 回答
0

在 Observable 中有三个选项。

  • 下一个 - 您订阅以获取数据
  • 错误 - 它是您传递给 subscribe 方法的下一个函数。它包含错误状态代码,主要是 http 错误,如连接错误、错误请求。
  • finally - 毕竟它执行任何方式。

还有其他的,例如 map,debounce 等。

  this.templateService.add(obj)
    .subscribe(
      (response) => {  // this is the next and hold the response
        console.log(response)
      },
      (error) => { // this is the error
        console.log(error)
      },
      (finall) => { // this is final and execute alwasys
        console.log('this is the final piece of code to execute')
      }
    );

我认为您的情况是,错误可能不会出现在 http 标准中,并且可能会出现在订阅中。

因此,请检查自定义消息是否接下来会出现,而不是作为错误出现。

于 2017-09-20T14:30:57.057 回答
0

您可以使用键入的响应

在您的error通知类型上,您可能有以下内容:

err =>  {
        this.localErrorResponse = err as ErrorResponse;
        this.message= this.localErrorResponse.message;
      }

之前,在你的课上:

import { ErrorResponse } from './error-response';
...
localErrorResponse: ErrorResponse;
message: string;

然后,你ErrorResponse可以像:

import { HttpHeaders} from './http-headers';

export class ErrorResponse{
  error: any;
  headers: HttpHeaders;
  message: string;
  name: string;
  ok: boolean;
  status: number;
  statusText: string;
  url: string;
}

和班级HttpHeaders

export class HttpHeaders {
  normalizedNames: any;
  lazyUpdate: any;
  lazyInit: any;
}

然后您可以映射一些其他变量,如message( this.message)

于 2018-04-11T16:31:31.117 回答