开发者问题收集

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

2021-05-24
85

我已经使用 angular 11 创建了在数组对象中搜索数据的函数。

它在 keydown 函数上显示错误。

创建搜索函数时显示控制台错误。

在 html 文件中

<input type="text" [(ngModel)]="archivedpollSearch" placeholder="{{ 'FIFTH_ITERATION.MY_COMMUNITIES.SEARCH_ARCHIVED_POLLS' | translate}}"
        class="search-input border-0 bg-transparent" (keydown)="searchArchivePolls()">



<mat-expansion-panel class="mat-elevation-z0 panelExpansion border-top border-bottom border-right border-left" *ngFor="let archives of archivedPoll;let i= index">
        <mat-expansion-panel-header>
          <mat-panel-title translate>{{archives?.question}}</mat-panel-title>
        </mat-expansion-panel-header>
        <mat-panel-description *ngFor="let options of archives?.CM_Poll_Options;let i=index">
          <div class="radio-group widthRadio" *ngIf="options?.pollId==archives?.id">
            <label class="container poll-done">
              {{options?.pollOption}}  
              <input  type="radio" name="polls" [checked]="archives?.CM_Poll_Results[i]?.choice==options?.id && archives?.CM_Poll_Results[i].userId ==this.userId">
              <span class="checkmark"></span>
              <small>{{(options?.percentageValue | number) || 0}} %</small>
              <div [style.width]="options.percentageValue + '%' "></div>
            </label>
      </div>
    </mat-panel-description>
  </mat-expansion-panel>

在我的 ts 文件中

searchArchivePolls(){  
    if(this.archivedpollSearch.length >= 1){
      this.archivedPoll.map((poll, index)=>{
        if(poll.question.includes(this.archivedpollSearch)){              
          this.archivedPoll = poll; 
          console.log(this.archivedPoll);     
        }
      })
    }else{
      this.getArchivePolls();
    }
  }

在 ngOninit 中调用的 getArchivePolls 函数

我需要使用搜索来搜索数组对象值。

这是我的服务器端响应

[
    {
        "id": 179,
        "question": "archived poll2?",
        "communityId": 198,
        "userPollCount": 1,
        "CM_Poll_Options": [
            {
                "id": 394,
                "pollId": 179,
                "pollOption": "archived1",
                "percentageValue": "100.0000"
            },
            {
                "id": 395,
                "pollId": 179,
                "pollOption": "archived2",
                "percentageValue": "0.0000"
            },
            {
                "id": 396,
                "pollId": 179,
                "pollOption": "archived3",
                "percentageValue": "0.0000"
            },
            {
                "id": 397,
                "pollId": 179,
                "pollOption": "archived4",
                "percentageValue": "0.0000"
            }
        ],
        "CM_Poll_Results": [
            {
                "userId": 6,
                "choice": 394,
                "CM_Users": {
                    "id": 6,
                }
            }
        ]
    },
    {
        "id": 178,
        "question": "testing question for archive polls?",
        "communityId": 198,
        "userPollCount": 0,
        "CM_Poll_Options": [
            {
                "id": 392,
                "pollId": 178,
                "pollOption": "testing1",
                "percentageValue": null
            },
            {
                "id": 393,
                "pollId": 178,
                "pollOption": "testing2",
                "percentageValue": null
            }
        ],
        "CM_Poll_Results": []
    }
]
1个回答

您正在尝试使用 ngfor 循环 ,但是您将其分配给对象:

684099887 < P>我认为您正在尝试从 ArchivedPoll 创建一个新数组,因此这可能更好: 348287015
eko
2021-05-24