【发布时间】: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}