传递数组并得到:错误错误:NG0900:尝试比较“[对象对象]”时出错。仅允许数组和可迭代对象

发布于 2025-01-11 12:50:33 字数 841 浏览 0 评论 0 原文

我正在开发 Angular 上的 CRUD 应用程序的前端,在传递数组时出现此错误:

错误 错误:NG0900:尝试比较“[object Object]”时出错。仅允许数组和可迭代对象

我该如何解决该错误?

这是我列出城市的方法:

listar(pagina:number = 0){
    this.filtro.estado = this.estadoSelected.name;
    this.filtro.page = pagina;
    this.cidadeService.listar(this.filtro).subscribe(data => {
      const contentKey:any = Object.values(data)[0];
      const registros:any = Object.values(data)[2];
      this.totalRegistros = registros;
      this.cidades = contentKey;
      console.log(Array.isArray(this.cidades))
      console.log(this.cidades);
    });
  }

这是我从 console.log

获得的内容在此处输入图像描述

非常感谢任何类型的帮助

I'm developing the front-end of a crud application on angular, and i got this error when passing an array:

ERROR Error: NG0900: Error trying to diff '[object Object]'. Only arrays and iterables are allowed

How can i solve the error?

Here's my method to list the cities:

listar(pagina:number = 0){
    this.filtro.estado = this.estadoSelected.name;
    this.filtro.page = pagina;
    this.cidadeService.listar(this.filtro).subscribe(data => {
      const contentKey:any = Object.values(data)[0];
      const registros:any = Object.values(data)[2];
      this.totalRegistros = registros;
      this.cidades = contentKey;
      console.log(Array.isArray(this.cidades))
      console.log(this.cidades);
    });
  }

This is what i get from console.log

enter image description here

Any type of help is very appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2025-01-18 12:50:33

该表有一个不同的组件,因此我解决了在 table.component.ts 上放置 @Input 来接收数组“cidades”table.component.ts 的值的问题

@Input() tabela!: any[];

table.component.html:

city.component.html

There's an different component for the table, so I solved the problem putting an @Input on the table.component.ts to receive the value of my array 'cidades'

table.component.ts:

@Input() tabela!: any[];

table.component.html:

<p-table [value]="tabela" ...

cities.component.html

<app-tabela-cidades [tabela]="cidades" [size]="filtro.size"></app-tabela-cidades>

铁憨憨 2025-01-18 12:50:33

错误 错误:NG0900:尝试比较时出错“”。仅允许数组和可迭代对象

在我的例子中,我有一个包含在表单组中的自定义组件。
错误在于表单组中的标准值是字符串而不是预期的数组。

const formGroup: any = {
  tags: [''], <--
};
this.generatorForm = this.fb.group(formGroup);

因此,该表单试图将某些内容与字符串而不是数组进行比较。
为了解决这个问题,我只是在那里添加了一个数组。

const formGroup: any = {
  tags: [[]], <--
};
this.generatorForm = this.fb.group(formGroup);

ERROR Error: NG0900: Error trying to diff ''. Only arrays and iterables are allowed

In my case, I had a custom component that was included in the Form Group.
The error was that instead of the standard value in the Form Group is a string instead of an array as expected.

const formGroup: any = {
  tags: [''], <--
};
this.generatorForm = this.fb.group(formGroup);

So the form is trying to compare something with a string instead of an array.
To fix this I just added an array there.

const formGroup: any = {
  tags: [[]], <--
};
this.generatorForm = this.fb.group(formGroup);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文