首页 > 解决方案 > 无法使用带有ngrx的角度读取未定义的属性“地图”

问题描述

我需要帮助来理解当我尝试使用来自 store 的 Observable 数组时发生的这个错误。

这是控制台错误

ERROR TypeError: Cannot read property 'map' of undefined
    at ngrx-entity.js:67
    at ngrx-store.js:1198
    at memoized (ngrx-store.js:1039)
    at defaultStateFn (ngrx-store.js:1079)
    at ngrx-store.js:1207
    at memoized (ngrx-store.js:1039)
    at ngrx-store.js:1198
    at memoized (ngrx-store.js:1039)
    at defaultStateFn (ngrx-store.js:1079)
    at ngrx-store.js:1207

这是我进行调度并尝试使用 Observable 的地方

  template: `
    <bc-patient-search> </bc-patient-search>
    <ngx-spinner [fullScreen]="false"></ngx-spinner>
    <bc-patient-preview-list [patients]="vm$ | async">
    </bc-patient-preview-list>
  `,
})
export class FindPatientPageComponent {
  patients$: Observable<Patient[]>;
  vm$ : Observable<Patient[]>;
  
  constructor(
    private store: Store<fromPatients.PatientsState>,
    private apiPatientService: ApiPatientsService,
    private spinner: NgxSpinnerService
  ) {  
    this.vm$ = store.pipe(select(fromPatients.selectPatientsTotal));
  }

  ngOnInit() {
    this.spinner.show();
    this.patients$ = this.apiPatientService.searchPatientList();
    this.loadPatients();
    setTimeout(() => {
      this.spinner.hide();
    }, 2000);
  }

  loadPatients() {
    this.store.dispatch(PatientLoadActions.loadPatients());
  }
}

这是选择器

export const selectPatientsTotal = createSelector(
  selectAllPatients,
  patients => patients.filter(patients => patients != null && patients != undefined)
);

export const selectAllPatients = createSelector(
  selectPatientState,
  fromPatients.selectAll
);

和减速机

  on(PatientLoadActions.loadPatientsSuccess, (state, action) =>
    adapter.setAll(action.patients, state)
  ),

标签: angularngrxngrx-storengrx-effectsngrx-entity

解决方案


是特色商店吗?然后你需要创建你的基本特征选择器:

export const selectPatientsState = createFeatureSelector(patientsFeatureKey);

比较https://ngrx.io/guide/entity/adapter,最后一个代码片段 (index.ts)。我想我记得我第一次使用 ngrx 实体时的相同错误。


推荐阅读