Search code examples
javascriptangularentitycrudngrx

NGRX entity updateOne not working: id undefined


I decided to ask for help, I just cannot get my head around NGRX Entity! (This code was created initially by NX ).

I have followed the NGRX Entity guide, I have also looked at loads of tutorial videos but I still cannot get NGRX Entity updateOne to work. Getting this error below - I can load the entities into the store with no issue, and these are building my UI fine.

I have an Entity collection of buttons and want to update the Store State of a button when clicked - that's all!

(any ideas why this is not working??)

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) 

This is the code I have so far:

// 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();



Solution

  • You're dispatching your action incorrectly.

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

    should be

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