【发布时间】:2019-08-21 12:45:12
【问题描述】:
我从一个 get 请求返回一个 json 对象。我尝试使用 forEach 遍历它,但出现错误:“Employee”类型上不存在属性“forEach”。员工是可观察的。
这是在 Angular 7 中
employee: Employee[];
single: any = [
{
"name": [],
"value": []
}
];
chartData(id){
return this.employeeService.getEmpSkill(id)
.subscribe( data => data.forEach((i) => {this.single.push({name: (i.skill_name), value: (i.score) })
this.single = [...this.single]
console.log(this.single)
}));
}
我希望返回一个数组,以便将数据写入图表。相反,我得到:error TS2339: Property 'forEach' does not exist on type 'Employee'。
【问题讨论】:
-
您的服务似乎返回
Observable<Employee>而不是Observable<Employee[]>。您应该检查您的类型和 api 以确保您正确解释了响应。 -
是的,就是这样。谢谢!一旦我被允许,我会标记为答案。
标签: json angular typescript foreach observable