【问题标题】:Cannot find a differ supporting object angular 7找不到不同的支撑物体 angular 7
【发布时间】:2018-11-14 13:50:41
【问题描述】:

您好,我正在使用 Angular 7

我创建了服务 user-report.service.ts 获取方法不在浏览器中打印

getReport() {
        /*this.http.get(`${this.apiUrl}/report`)
            .subscribe(res => console.log(res._body));*/
        return this
           .http
           .get(`${this.apiUrl}/report`);
    }

user-report.component.ts

getReport() {
        this.userReport.getReport()
            .subscribe((data: UserReport[]) => {
            this.reports = data;
            console.log(data);
    });
    }

模板

<table class="table table-hover">
  <thead>
  <tr>
      <td>User Name</td>
      <td>Created Date</td>
      <td>Updated Date</td>
      <td>Time</td>
      <td>Status</td>
  </tr>
  </thead>

  <tbody>
      <tr *ngFor="let report of reports">
          <td>{{ report.username }}</td>
          <td>{{ report.created_date }}</td>
          <td>{{ report.updated_date }}</td>
          <td>{{ report.time }}</td>
          <td>{{ report.status }}</td>

      </tr>
  </tbody>
</table>

但我遇到了这样的错误

错误错误:找不到不同的支持对象“响应状态:200 OK for URL:http://localhost:3000/report”类型为“object”。 NgFor 只支持绑定到数组等 Iterables。

不知道是什么问题

【问题讨论】:

  • 检查 this.userReport.getReport()... 它没有返回一个数组 - 它是返回一个单一的报告吗?尝试在浏览器中测试 URL。仅供参考,仅仅因为您将对象转换为数组,并没有做到这一点。
  • NgFor 仅支持绑定到 Iterables,例如 Arrays。
  • 您在数据中收到了什么?是数组还是对象?
  • 我的数据响应 {headers: Headers {_headers: Map(1), _normalizedNames: Map(1)} ok: true status: 200 statusText: "OK" type: 2 url: "localhost:3000 /report" _body: "[{"_id":"5bec00c1043e8b3070858e36","username":"NAME","created_date":"2018 年 11 月 14 日","updated_date":"2018 年 11 月 14 日","时间": "4:32 PM","status":"D","v":0},,]" __proto: Body}

标签: angular angular7


【解决方案1】:

将单个报告转换为数组的简单修复:

getReport() {
        this.userReport.getReport()
            .subscribe((data: UserReport) => {
            this.reports = [data];
            console.log(data);
    });
    }

【讨论】:

  • this.reports = [data._body];它工作正常,但在表格中打印了注释
  • 为什么是data._body? _body 是您在 json 中返回的属性吗?
  • headers: Headers {_headers: Map(1), _normalizedNames: Map(1)} ok: true status: 200 statusText: "OK" type: 2 url: "localhost:3000/report" _body: " [{"_id":"5bec00c1043e8b3070858e36","username":"NAME","created_date":"2018 年 11 月 14 日","updated_date":"2018 年 11 月 14 日","time":"4:32 PM" ,"status":"D","v":0},,]" __proto: 正文
  • 看你的json,_body已经是一个数组了。试试this.reports = data._body;
  • 我认为如果我打印这个 {{reports}} 它打印但 ngFor 不工作,循环是一个问题
猜你喜欢
  • 2018-09-29
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 2020-02-08
  • 2019-04-29
相关资源
最近更新 更多