首页 > 解决方案 > 将 HttpErrorResponse 值解析为 JSON 对象

问题描述

我有以下HttpErrorResponse

在此处输入图像描述

我需要value通过解析 JSON 对象从属性中获取状态和消息

我试过以下

this.http.request(uploadReq).subscribe(event => {
      if (event.type === HttpEventType.UploadProgress)
        this.progress = Math.round(100 * event.loaded / event.total);
      if (this.progress == 100) {
        this.uploadeda = true;
        this.error = false;
      }
      else if (event.type === HttpEventType.Response)
        this.message = event.body.toString();

    }, err => { console.error(JSON.parse(err.value.toString()).status);});

我不断收到以下错误

ERROR TypeError: Cannot read property 'toString' of undefined

标签: angulartypescriptangular8

解决方案


您不需要解析错误响应。此外,该值位于错误对象内。所以 err.error.value 会给你预期的


推荐阅读