【问题标题】:NGRX entity updateOne not working: id undefinedNGRX 实体 updateOne 不工作:id 未定义
【发布时间】:2020-09-18 07:16:00
【问题描述】:

我决定寻求帮助,我无法理解 NGRX Entity! (此代码最初由 NX 创建)。

我已按照 NGRX Entity 指南进行操作,也看过大量教程视频,但我仍然无法让 NGRX Entity updateOne 工作。 在下方出现此错误 - 我可以毫无问题地将实体加载到商店中,并且这些都可以很好地构建我的 UI。

我有一个实体按钮集合,并希望在单击时更新按钮的存储状态 - 仅此而已!

(任何想法为什么这不起作用??)

ERROR TypeError: Cannot read property 'id' of undefined
    at http://localhost:4200/vendor.js:83815:26
    at Array.filter (<anonymous>)
    at updateManyMutably (http://localhost:4200/vendor.js:83811:27)
    at updateOneMutably (http://localhost:4200/vendor.js:83801:16)
    at Object.operation [as updateOne] (http://localhost:4200/vendor.js:83622:27)
    at http://localhost:4200/main.js:1169:28
    at http://localhost:4200/vendor.js:88532:26
    at reducer (http://localhost:4200/main.js:1173:12)
    at http://localhost:4200/vendor.js:87072:20
    at combination (http://localhost:4200/vendor.js:86960:37) 

这是我目前的代码:

// state
export interface QuickButton {
    id: number;
    isSelected: boolean;
    title: string;
    linkUrl: string;
}
// in component
this.store.dispatch( actions.setQuickFilter( evt ) );

// evt = {id: 1, isSelected: true, linkUrl: "", title: "Video"}
// in actions
export const setQuickFilter = createAction(
  '[QuickBar] setQuickFilter',
  props<{update: Update<QuickButton>}>()
);
// in reducer
export const QUICKBAR_FEATURE_KEY = 'quickBar';

export interface State extends EntityState<QuickButton> {
  selectedId?: string | number; // which QuickBar record selected
  loaded: boolean; // has the QuickBar list been loaded
  error?: string | null; // last none error (if any)
}

export interface QuickBarPartialState {
  readonly [QUICKBAR_FEATURE_KEY]: State;
}

export const quickBarAdapter: EntityAdapter<QuickButton> = createEntityAdapter<QuickButton>();

export const initialState = quickBarAdapter.getInitialState({
  // set initial required properties
  loaded: false,
});

const quickBarReducer = createReducer(
  initialState,

  on(QuickBarActions.loadQuickBarSuccess, (state, action) =>
    quickBarAdapter.addAll( action.quickBar, state )
  ),
  on(QuickBarActions.loadQuickBarFailure, (state, { error }) => ({
    ...state,
    error,
  })),

  on(QuickBarActions.setQuickFilter, (state, {update}) => {
      /// **** This is NOT Working *****
      return quickBarAdapter.updateOne( update, state);
    } 
  )
);

export function reducer(state: State | undefined, action: Action) {
  return quickBarReducer(state, action);
}

export const {
  selectIds,
  selectEntities,
  selectAll,
  selectTotal,
} = quickBarAdapter.getSelectors();


【问题讨论】:

    标签: javascript angular entity crud ngrx


    【解决方案1】:

    您的操作发送不正确。

    this.store.dispatch(actions.setQuickFilter(evt));
    

    应该是

    this.store.dispatch(actions.setQuickFilter({ update: evt }));
    

    【讨论】:

    • 太棒了 - 我自己也想通了 - 无论如何感谢您抽出宝贵时间提供帮助 :)
    【解决方案2】:

    耶!!最后。 这是一个真正愚蠢的错误——因为不理解实体。 大量的反复试验和记录来解决这个问题!

    解决方案: 将组件中的调度调用更改为:

    this.store.dispatch( actions.setQuickFilter( {update: evt} } ) );
    

    到:

    this.store.dispatch( actions.setQuickFilter( {update: {id: evt.id, changes: evt} } ) );
    

    现在我订阅的所有功能都可以使用按钮中的更新值来控制自己的 UI 元素。终于!

    【讨论】:

    • 这并不是不理解ngrx entity,而是更多的是纯TypeScript / Javascript的错误。如果您在代码编辑器中使用 linter 或 helper,则不应发生此类错误。例如,如果我在我的一个应用程序中使用 VS Code 会捕获此错误!值得研究! :)
    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 2022-01-21
    • 1970-01-01
    • 2022-10-05
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    相关资源
    最近更新 更多