【问题标题】:Angular - Get Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed错误:尝试比较“[object Object]”时出错。只允许使用数组和可迭代对象 - 我的情况没有答案
【发布时间】:2022-01-16 15:00:26
【问题描述】:

我现在正在 Angular 上构建我的前端,我一生都看不到导致此错误的原因。我使用 Node.js 作为我的后端。下面是我的代码。

国家.ts

export class Country {
  id?: string;
  name?: string;
  icon?: string;
  imageFlag?: string;
}

countries.services.ts

export class CountriesService {
  constructor(private http: HttpClient) {}

  getCountries(): Observable<Country[]> {
    return this.http.get<Country[]>('http://127.0.0.1:3300/api/v1/countries/');
  }
}

countries-list.component.ts

export class CountriesListComponent implements OnInit {
  countries: Country[] = [];

  constructor(private countriesService: CountriesService) {}

  ngOnInit(): void {
    this.countriesService.getCountries().subscribe((amazwe) => {
      this.countries = amazwe;
      console.log(amazwe);
    });
  }
}

我在console.log(amazwe) 上的控制台描述如下

{
    "status": "success",
    "results": 7,
    "data": {
        "data": [
            {
                "_id": "61c4827a69a96679469adac2",
                "name": "Botswana",
                "icon": "Safari iCON",
                "imageFlag": "Botswana Flag",
                "__v": 0,
                "id": "61c4827a69a96679469adac2"
            },
            {
                "_id": "61c482ab69a96679469adac4",
                "name": "South Africa",
                "icon": "Safari iCON",
                "imageFlag": "South Africa Flag",
                "__v": 0,
                "id": "61c482ab69a96679469adac4"
            },
            {
                "_id": "61c482c369a96679469adac6",
                "name": "Namibia",
                "icon": "Safari iCON",
                "imageFlag": "Namibia Flag",
                "__v": 0,
                "id": "61c482c369a96679469adac6"
            },
            {
                "_id": "61c482f569a96679469adac8",
                "name": "Mozambique",
                "icon": "Safari iCON",
                "imageFlag": "Mozambique Flag",
                "__v": 0,
                "id": "61c482f569a96679469adac8"
            },
            {
                "_id": "61c4831769a96679469adaca",
                "name": "Zimbabwe",
                "icon": "Safari iCON",
                "imageFlag": "Zimbabwe Flag",
                "__v": 0,
                "id": "61c4831769a96679469adaca"
            },
            {
                "_id": "61c4832f69a96679469adacc",
                "name": "Zambia",
                "icon": "Safari iCON",
                "imageFlag": "Zambia Flag",
                "__v": 0,
                "id": "61c4832f69a96679469adacc"
            },
            {
                "_id": "61c4836369a96679469adace",
                "name": "Tanzania",
                "icon": "Safari iCON",
                "imageFlag": "Tanzania Flag",
                "__v": 0,
                "id": "61c4836369a96679469adace"
            }
        ]
    }
}

【问题讨论】:

    标签: node.js angular angular-httpclient


    【解决方案1】:

    您对类/接口的响应数据如下:

    export interface ICountryListResponse {
      status: string;
      results: number;
      data: { data: Country[] };
    }
    

    修改您的CountriesService.getCountries() 以期望收到ICountryListResponse 数据响应,然后使用.pipe(map) 将结果返回为Observable&lt;Country[]&gt;

    countries.services.ts

    import { map } from 'rxjs/operators';
    
    getCountries(): Observable<Country[]> {
      return this.http
        .get<ICountryListResponse>('http://127.0.0.1:3300/api/v1/countries/')
        .pipe(map((response: ICountryListResponse) => response.data.data));
    }
    

    Sample Demo on StackBlitz

    【讨论】:

    • 您的解决方案引入了 3 个错误。错误 1. 模块 '"@tourafrika/tours"' 在 countries-list.component.ts 文件中没有导出成员 'Country' 指向从 '@tourafrika/tours' 导入 { CountryService, Country };我确实根据此代码将我的 country.ts 文件导出到我的 index.ts 文件中 export * from './lib/models/country';我有一个@NgModule - exports = [ShellComponent, SidebarComponent, countriesListComponent] 错误 2:在 country.ts 中找不到名称“Country”。数据:{数据:国家[]};错误 3:在 countries.service.ts 中找不到名称“Country”。 getCountries(): Observable {
    • 您是否将 Country 类导入到每个需要的类中?并且请在StackBlitz 上创建一个最小的可重现示例,以便于调试。谢谢。
    • 谢谢勇。现在一切正常!我已经用你的 ICountryListResponse 接口替换了我最初的 Country Class。在我的培训中,有人指示我可以使用接口或类声明来创建模型,在我的情况下,这将是 country.ts 模型。所以我急忙用你的 ICountryListResponse 接口替换我现有的 Country Class。在查看我的代码时,我选择了我的错误,现在一切正常。一百万谢谢。这对我来说是一个很好的学习曲线。
    【解决方案2】:

    使用 JSON 格式化程序(例如:https://codebeautify.org/jsonviewer)有助于更好地查看数据:

    {
      "status": "success",
      "results": 7,
      "data": {
        "data": [
          {
            "_id": "61c4827a69a96679469adac2",
            "name": "Botswana",
            "icon": "Safari iCON",
            "imageFlag": "Botswana Flag",
            "__v": 0,
            "id": "61c4827a69a96679469adac2"
          },
          {
            "_id": "61c482ab69a96679469adac4",
            "name": "South Africa",
            "icon": "Safari iCON",
            "imageFlag": "South Africa Flag",
            "__v": 0,
            "id": "61c482ab69a96679469adac4"
          },
          {
            "_id": "61c482c369a96679469adac6",
            "name": "Namibia",
            "icon": "Safari iCON",
            "imageFlag": "Namibia Flag",
            "__v": 0,
            "id": "61c482c369a96679469adac6"
          },
          {
            "_id": "61c482f569a96679469adac8",
            "name": "Mozambique",
            "icon": "Safari iCON",
            "imageFlag": "Mozambique Flag",
            "__v": 0,
            "id": "61c482f569a96679469adac8"
          },
          {
            "_id": "61c4831769a96679469adaca",
            "name": "Zimbabwe",
            "icon": "Safari iCON",
            "imageFlag": "Zimbabwe Flag",
            "__v": 0,
            "id": "61c4831769a96679469adaca"
          },
          {
            "_id": "61c4832f69a96679469adacc",
            "name": "Zambia",
            "icon": "Safari iCON",
            "imageFlag": "Zambia Flag",
            "__v": 0,
            "id": "61c4832f69a96679469adacc"
          },
          {
            "_id": "61c4836369a96679469adace",
            "name": "Tanzania",
            "icon": "Safari iCON",
            "imageFlag": "Tanzania Flag",
            "__v": 0,
            "id": "61c4836369a96679469adace"
          }
        ]
      }
    }
    

    看起来你正在取回你想要的数据,但它只是有一个包装器。

    this.countries = amazwe.data.data;

    ...会给你我怀疑你正在寻找的结果,但你必须改变返回值的类型来处理这个。

    【讨论】:

    • 感谢您对使用 JSON 格式化程序的提醒。这是我的第一篇文章,仍在寻找合适的按钮选项/使用。
    • 是的,没错,console.log(amazwe) 上的数据确实是我想反映在前端的数据。将我的代码更改为 this.countries = amazwe.data.data;引入了一个新错误,指出“国家 []”类型上不存在属性“数据”。 16 this.countries = amazwe.data.data;这个错误在我的国家列表.component.ts ~~~~
    • 是的,正如我所说,需要更新类型定义以匹配您返回的数据结构。
    • 感谢 kshetline。您的解决方案也确实有效。
    猜你喜欢
    • 2020-01-11
    • 2020-04-05
    • 2018-12-19
    • 2021-01-09
    • 2018-09-14
    • 2019-10-26
    • 2020-08-15
    • 1970-01-01
    相关资源
    最近更新 更多