【问题标题】:Getting a "TypeError: Cannot read property 'filter' of undefined" when trying to search a list尝试搜索列表时出现“TypeError:无法读取未定义的属性‘过滤器’”
【发布时间】:2021-07-17 17:16:27
【问题描述】:

HTML

<ion-content fullscreen>
  <!-- Searchbar with a placeholder -->
  <ion-searchbar
    (ionInput)="filterList($event)"
    placeholder="Zoek een locatie"
  ></ion-searchbar>

  <ion-grid>
    <ion-row>
      <!-- locatie cards -->
      <ion-col class="row1" size="11">
        <ion-list lines="none">
          <ion-item *ngFor="let location of locations">
            <ion-card class="locatieCard">
              <ion-item>
                <img
                  class="locatieImg"
                  src="assets/spar_img.jpg"
                  slot="start"
                />
                <ion-grid>
                  <ion-row>
                    <ion-card-subtitle>{{ location.Name }}</ion-card-subtitle>
                  </ion-row>
                  <ion-row>
                    <ion-button
                      size="small"
                      fill="clear"
                      (click)="presentPopover($event, location.Contact)"
                    >
                      Meer info
                    </ion-button>
                  </ion-row>
                </ion-grid>
              </ion-item>
            </ion-card>
          </ion-item>
        </ion-list>
      </ion-col>

      <ion-col class="row2" size="1"> ion col 2 </ion-col>
    </ion-row>
  </ion-grid>
</ion-content>

TS

import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { ToolbarTitleService } from '../Services/toolbar-title.service';
import { IonSearchbar, PopoverController } from '@ionic/angular';
import { PopoverComponent } from '../popover/popover.component';
import { SyncServiceService } from '../Services/sync-service.service';
import { UserService } from '../Services/user.service';
import { first } from 'rxjs/operators';

@Component({
  selector: 'app-locaties',
  templateUrl: './locaties.component.html',
  styleUrls: ['./locaties.component.css'],
})
export class LocatiesComponent implements OnInit {
  public locations: any[];
  user;
  public locationsBackup: any[];

  constructor(
    private toolbarTitle: ToolbarTitleService,
    public popoverController: PopoverController,
    private syncService: SyncServiceService,
    private userService: UserService
  ) {}

  async ngOnInit() {
    this.toolbarTitle.setToolbarTitle('Locaties');
    this.user = await this.userService.getUser();
    // Haalt alle shops van de gebruiker op en zet ze in locations
    this.locations = await this.syncService.getShops(this.user);
    this.locations = await this.initializeItems();
  }

  // Popover
  async presentPopover(ev: any, Contact: any) {
    const popover = await this.popoverController.create({
      component: PopoverComponent,
      componentProps: {
        phones: Contact.Phones[0].Number,
        email: Contact.Email,
        street: Contact.Addresses[0].Street1,
        city: Contact.Addresses[0].City,
      },
      event: ev,
      translucent: true,
    });
    return await popover.present();
  }

  // Search
  async initializeItems(): Promise<any> {
    this.locations = await this.syncService
      .getShops(this.user)
      .valueChanges()
      .pipe(first())
      .toPromise();
    this.locationsBackup = this.locations;
    return this.locations;
  }

  // Filter
  async filterList(evt) {
    this.locations = this.locationsBackup;
    const searchTerm = evt.srcElement.value;

    if (!searchTerm) {
      return;
    }

    this.locations = this.locations.filter((currentLocation) => {
      if (currentLocation.Name && searchTerm) {
        return (
          currentLocation.Name.toLowerCase().indexOf(searchTerm.toLowerCase()) >
          -1
        );
      }
    });
  }
}


我正在尝试筛选商店列表。我不断收到“无法读取未定义的属性'过滤器'”错误。我似乎无法弄清楚我必须改变什么才能让它发挥作用。 Locations 基本上是一个包含商店对象的数组。有人知道该怎么做吗?我知道它必须与 filterList 函数有关。

【问题讨论】:

    标签: angular ionic-framework filter


    【解决方案1】:

    直到getShops() 返回值,locations 是未定义的(因此:无法读取未定义的属性过滤器)。

    你可以将它初始化为一个空数组:

    locations = [];
    

    或检查它在filterList 中是否未定义。

    另外,initializeItems 永远不会被调用,因此locationsBackup 也是如此。

    【讨论】:

    • 我已将initializeItems 放入 ngOnInit 并将位置初始化为一个空数组,但它仍然给我同样的错误
    • @FurkanÖztürk 你能更新问题中的代码吗?
    • 刚刚更新了。添加了更改
    • @FurkanÖztürk 它还没有初始化为任何值,它现在只有一个类型声明。您需要逐字分配值:locations: any[] = []backupLocations 相同)。
    • 这行得通,但现在我得到一个不同的错误:TypeError: this.syncService.getShops(...).valueChanges is not a function
    猜你喜欢
    • 2021-12-26
    • 2019-03-26
    • 2021-10-31
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2019-05-01
    • 1970-01-01
    相关资源
    最近更新 更多