小编Joe*_*ven的帖子

Angular 为什么这个 HTTP 请求响应数组的长度未定义?

我正在尝试获取包含客户可以加入的大厅信息的数据。我从 springboot API 获取这些数据。对于要在我的 angular 前端中显示的数据,我将结果添加到作为组件属性的 Set 中。但是从 API 获取此数据并将其映射到对象数组后,我无法遍历结果。都是因为数组的长度被称为未定义。

我正在使用最新版本的 Angular(目前是 7)并且已经尝试使用 map 方法以不同的方式映射 JSON 响应。而不是使用订阅功能。同样直接将响应分配给其他数组会产生此错误:LobbyComponent.html:10 错误错误:找不到类型为“object”的不同支持对象“[object Object]”。NgFor 仅支持绑定到可迭代对象,例如数组。

组件

export class LobbyComponent implements OnInit {

  lobbies: Set<Lobby>;
  constructor(private lobbyService: LobbyService) { }

  getLobbies(): void {
    this.lobbyService.getLobbies().subscribe(response => {

      console.log(response);


      // This solutions give this error: ERROR TypeError: response.forEach is not a function
      // response.forEach(element => console.log(element.id)) 

      //Todo: fix response.length is undifenided
      console.log(response.length)
      for (var i = 0; i < response.length; i++) {
        console.log(i);
        this.lobbies.add(response[i])
      } …
Run Code Online (Sandbox Code Playgroud)

arrays json http typescript angular

1
推荐指数
1
解决办法
1007
查看次数

标签 统计

angular ×1

arrays ×1

http ×1

json ×1

typescript ×1