类型对象上不存在属性长度

1 typescript angular

我有一个从docker获取JSON的服务。我想获取容器的数量。

因此,我订阅了我的服务:

ngOnInit() {
    this.docker.getContainers().subscribe(containers => {
        console.log(containers.length);
    });
}
Run Code Online (Sandbox Code Playgroud)

我在控制台上得到了正确的答案,但是我得到了:

类型对象上不存在属性长度

难道我做错了什么?

export class DockerService {
  private baseUrl:string = 'http://localhost:8080/api/docker';
  private headers = new HttpHeaders({'Content-Type':'application/json'});
  private options = new HttpHeaderResponse({headers:this.headers});

  constructor(private _http:HttpClient) { }

  getContainers() {
    return this._http.get(this.baseUrl + "/containers", this.options);
  }

  getImages() {
    return this._http.get(this.baseUrl + "/images", this.options);
  }
}
Run Code Online (Sandbox Code Playgroud)

Ign*_*Ara 6

在声明var 时应使用(containers:any [])

ngOnInit() {
    this.docker.getContainers().subscribe((containers: any[]) => {
        console.log(containers.length);
    });
}
Run Code Online (Sandbox Code Playgroud)


小智 0

你应该映射你的回应,试试这个:

getContainers() {
    return this._http.get(this.baseUrl + "/containers", this.options).map(res => {
                return res.json();
            });
}



getImages() {
    return this._http.get(this.baseUrl + "/images", this.options).map(res => {
                return res.json();
            });
}
Run Code Online (Sandbox Code Playgroud)