简体   繁体   English

Angular 问题:找不到支持“object”类型的 object“[object Object]”的不同。 NgFor 仅支持绑定到诸如 Arrays 之类的 Iterables

[英]Angular problem: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays

I am pretty new with Angular and I stuck with problem building up my portfolio project.我对 Angular 很陌生,但在构建我的投资组合项目时遇到了问题。 The problem is with receiving list of (nested) objects like this:问题在于接收这样的(嵌套)对象列表:

    "$id": "1",
    "data": {
        "$id": "2",
        "$values": [
            {
                "$id": "3",
                "id": 1,
                "name": "producto",
                "shortDescription": "prod11111",
                "longDescription": "lorem ipsum bla bla",
                "category": "1",
                "price": 50.00,
                "stockLevel": 10,
                "orderDetails": {
                    "$id": "4",
                    "$values": []
                }
            },
            {
                "$id": "5",
                "id": 2,
                "name": "segundo",
                "shortDescription": "prod222",
                "longDescription": "lorem ipsum",
                "category": "2",
                "price": 30.00,
                "stockLevel": 20,
                "orderDetails": {
                    "$id": "6",
                    "$values": []
                }
            }
        ]
    },
    "error": null
}

This is my app.component.ts:这是我的 app.component.ts:

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'Shop';
  products: any[] = [];
  
  constructor(private http: HttpClient) {}

  ngOnInit(): void {
    this.http.get('https://localhost:5001/api/products').subscribe((response: any) => {
      this.products = response.data;
    }, error => {
      console.log(error);
    });
  }
}

This is app.component.html:这是 app.component.html:

<app-nav-bar></app-nav-bar>

<div class="container" style="margin-top: 140px;">
    <h1>Welcome to {{title}}!</h1>
    <ul>  
        <li class="list-unstyled" *ngFor="let product of products">
            {{product.name}}
        </li>
    </ul>
</div>

I looked thru StackOverflow for similar problems but cannot resolve this in my case.我通过 StackOverflow 查看了类似的问题,但在我的情况下无法解决。 I think that there is problem because of arrays of arrays or nested objects.我认为由于 arrays 或嵌套对象的 arrays 存在问题。

Any help is appreciated!任何帮助表示赞赏!

The response.data is not an array. response.data不是数组。 Instead, I believe that response.data.$values is what you need for the products array.相反,我相信response.data.$values是您需要的products数组。

this.http.get('https://localhost:5001/api/products').subscribe((response: any) => {
  this.products = response.data.$values;
}, error => {
  console.log(error);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular - 找不到类型为“string”的不同支持对象“Item One”。 NgFor 只支持绑定到 Iterables,比如 Arrays - Angular - Cannot find a differ supporting object 'Item One' of type 'string'. NgFor only supports binding to Iterables such as Arrays Angular '找不到一个不同的支持 object '[object Object]' 类型的 'object'。 NgFor 仅支持绑定到 Iterables,例如 Arrays。 - Angular 'Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.' 找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 仅支持绑定到可迭代对象,例如数组。 [Angular 8] - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.[Angular 8] 错误:找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到Iterables - Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables ERROR 错误:“找不到类型为‘object’的不同支持对象‘[object Object]’。 NgFor 仅支持绑定到可迭代对象,例如数组。” - ERROR Error: “Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.” 我不断收到此错误:找不到不同的支持 object '[object Object]' 类型为 'object'。 NgFor 只支持 - I keep getting this ERROR: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports 在 Angular 8 中找不到不同的支持 object '[object Object] - Cannot find a differ supporting object '[object Object] in Angular 8 错误 错误:找不到不同的支持 object '[object Object]' 类型为 'object' Angular - ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object' Angular 找不到支持“object”类型的 object“[object Object]”的不同,但它已经是一个数组 - Cannot find a differ supporting object '[object Object]' of type 'object' , but it's already an array 迭代数组中的数组时出错:“找不到支持 object '[object Object]' 类型为 'object' 的不同。” - Error when iterating over array within array: “Cannot find a differ supporting object '[object Object]' of type 'object'.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM