You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular NG0900错误求助:API数据绑定表格失败排查

解决Angular NG0900错误:尝试对比'[object Object]'失败

错误信息

NG0900: 尝试对比'[object Object]'失败。仅允许数组与可迭代对象,使用了Observable与subscribe

问题场景

我尝试从API获取数据并传递到组件用于表格展示,但反复遇到同一问题。能看到数据存在,但存在异常。

相关代码

服务代码

public getProjects(): Observable<Project[]> {
  return this.http.get<CustomResponse>(`${this.apiUrl}/project/list`)
    .pipe(map(data => data.data)     // 后来改为 data.data.projects
    )
}

组件代码

ngOnInit(){ 
  //                        | 错误光标在第24行的.与s之间
  this.service.getProjects().subscribe((value: Project[])=>{
    this.projects= value;
    console.log(value);             //第26行
    console.log( typeof(value));    //第27行
    console.log(this.projects);     //第28行
  })
}

CustomResponse接口

export interface CustomResponse {
    timeStamp: Date;
    statusCode: number;
    status: string;
    reason: string;
    message: string;
    developerMessage: string;
    data:  Project[];          // 后来改为 data:any
}

Project接口

export interface Project{
    id: number;
    name: string;
    description: string;
}

尝试过的方案

试过修改map与pipe的使用方式,甚至重写函数,也试过网上的各种方案,但都无法解决问题,希望有人能说明原因。

问题解决(更新)

API返回的是包含数组的对象,之前在CustomResponse接口里把data定义为Project[]是错误的。尝试直接访问data中的projects数组时会报错,将data的类型改为any后,在map操作里返回data.data.projects就解决了问题。


内容的提问来源于stack exchange,提问作者Broderick Boucher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:18:26