«Невозможно прочитать свойство selectedZones of undefined» для уже инициализированной и не пустой переменной - PullRequest
0 голосов
/ 05 марта 2020

У меня есть компонент, который фильтрует зоны сборки с выбранными зонами в ngOnInit

rfid.component.ts

@Component(...)
export class RfidComponent implements OnInit {

  gridApi: GridApi;
  partList = new BehaviorSubject<IPart>(null);
  selectedZones: string[] = [];

  constructor(private zvSelectionService: ZvSelectionService) {
  }

  isInBuildZone(bom: IBomLine): boolean {
    return this.selectedZones.includes(bom.buildZone);
  }

  ngOnInit(): void {
    this.selectedZones = this.zvSelectionService.selectedZones.map(zone => zone.name);
    console.log(this.selectedZones) // -> ["LM3"]

    of(partList).pipe(
      map((list: IPart) => ({...list, bomLines: list.bomLines.filter(this.isInBuildZone)})),
      filter((list: IPart) => list.bomLines.length > 0),
    )
      .subscribe(data => this.partList.next(data));
  }
}

Но я получаю следующую ошибку даже если выбранные зоны инициализированы и не пусты:

ERROR TypeError: Cannot read property 'selectedZones' of undefined
    at isInBuildZone (rfid.component.ts:50)
    at Array.filter (<anonymous>)
    ...

1 Ответ

1 голос
/ 05 марта 2020

Вы должны связать функцию, вызывающую класс компонента

map((list: IPart) => ({...list, bomLines: list.bomLines.filter(this.isInBuildZone.bind(this))})),
...