简体   繁体   English

显示项目分页时,错误找不到支持 object 类型为“object”的“[object Object]”?

[英]error Cannot find a differ supporting object '[object Object]' of type 'object' when display items pagination?

I work on a web application with ASP.NET Core 6 and Angular 13.我使用 web 应用程序与 ASP.NET Core 6 和 Angular 13 一起工作。

This application displays a list of items successfully without any issue.此应用程序成功显示项目列表,没有任何问题。 My issue happens when there's pagination for the items displayed.当显示的项目有分页时,我的问题就会发生。

I get an error in the console:我在控制台中收到错误消息:

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
    at NgForOf.ngDoCheck (common.mjs:3174:31)
    at callHook (core.mjs:2561:1)
    at callHooks (core.mjs:2520:1)
    at executeCheckHooks (core.mjs:2452:1)
    at refreshView (core.mjs:9589:1)
    at refreshComponent (core.mjs:10777:1)
    at refreshChildComponents (core.mjs:9376:1)
    at refreshView (core.mjs:9630:1)
    at refreshEmbeddedViews (core.mjs:10731:1)
    at refreshView (core.mjs:9604:1)

What I tried is:我试过的是:

(1) Create Web API action to display all items (1)创建Web API动作显示所有item

public async Task<IActionResult> GetAll(int pageNumber = 1)
{
    var allitems = _iitem.GetAllItems();

    var result = _pageHelper.GetPage(allitems.AsQueryable(), pageNumber);

    var itemsdata = new ItemsPageViewModel
            {
                items = result.Items,
                Pager = result.Pager
            };

    return Ok(itemsdata);
}

When I call this action method, it returns json data as shown here:当我调用此操作方法时,它返回 json 数据,如下所示:

{
    "items": [
        {
            "id": 3,
            "itemNameER": "قلم",
            "itemNameEN": "pen",
            "description": "1"
        },
        {
            "id": 4,
            "itemNameER": "قلم",
            "itemNameEN": "pencil",
            "description": null
        },
        {
            "id": 5,
            "itemNameER": "قلم",
            "itemNameEN": "pen2",
            "description": null
        }
    ],
    "pager": {
        "numberOfPages": 1,
        "currentPage": 1,
        "totalRecords": 3
    }
}

(2) In Angular 13, I created a service in service.ts : (2) Angular 13,我在service.ts中创建了一个服务:

export class ErpServiceService {

  constructor( private http: HttpClient ) { }

  getAll(): Observable<any> {
    return this.http.get(baseUrl);
  }

(3) I created a component items logic in component.ts : (3)我在component.ts中创建了一个组件项逻辑:

ngOnInit(): void {
    this.retrieveAllItems();
  }
 
  retrieveAllItems(): void {
    this.erpservice.getAll()
      .subscribe(
        data => {
          this.items = data;
          console.log(data);
        },
        error => {
          console.log(error);
        });
  }

(4) I created a component view component.html : (4)我创建了一个组件视图component.html

<table id="customers">
  <tr>
      <th>Id</th>
      <th>ItemNameAR</th>
      <th>ItemNameEN</th>
      <th>Description</th>
  </tr>
  <tr *ngFor="let item of items">
      <td>{{item.id}}</td>
      <td>{{item.itemNameAR}}</td>
      <td>{{item.itemNameEN}}</td>
      <td>{{item.description}}</td>
  </tr>
</table>

How to apply pagination on item rows displayed?如何在显示的项目行上应用分页?

I think when you process the http-response from your backend, you mistakenly assign the response-object to this.items instead of the items-array that is wrapped in the response-object.我认为当您从后端处理 http 响应时,您错误地将响应对象分配给this.items而不是包装在响应对象中的项目数组。

Can you try the following?:您可以尝试以下操作吗?:

retrieveAllItems(): void {
  this.erpservice.getAll()
    .subscribe(
      data => {
        // Here you need to assign the array inside the data-object:
        this.items = data.items;
        console.log(data);
      },
      error => {
        console.log(error);
      });
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM