无法读取未定义的 属性 'type' (ngrx)

Cannot read property 'type' of undefined (ngrx)

我正在使用 ngrx。我收到错误

Cannot read property 'type' of undefined

这是我的部分代码:

@Effect() foo$ = this.actions$
    .ofType(Actions.FOO)
    .withLatestFrom(this.store, (action, state) => ({ action, state }))
    .map(({ action, state }) => {
      if (state.foo.isCool) {
        return { type: Actions.BAR };
      }
    });

这个问题有点棘手,根据报错不容易定位问题

在这种情况下,当 state.foo.isCoolfalse 时,不会返回任何操作。

所以改成这样可以解决问题:

@Effect() foo$ = this.actions$
    .ofType(Actions.FOO)
    .withLatestFrom(this.store, (action, state) => ({ action, state }))
    .map(({ action, state }) => {
      if (state.foo.isCool) {
        return { type: Actions.BAR };
      }

      return { type: Actions.SOMETHING_ELSE };
    });