NGRX 实体 updateOne 不工作:id undefined
NGRX entity updateOne not working: id undefined
我决定寻求帮助,但我无法理解 NGRX 实体! (此代码最初由 NX 创建)。
我遵循了 NGRX 实体指南,我也看了很多教程视频,但我仍然无法使 NGRX 实体 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();
您发送的操作不正确。
this.store.dispatch(actions.setQuickFilter(evt));
应该是
this.store.dispatch(actions.setQuickFilter({ update: evt }));
耶!!最后。
这是一个真正的愚蠢错误——因为不理解实体。
大量的反复试验和记录来解决这个问题!
解决方法:
更改组件中的调度调用 from:
this.store.dispatch( actions.setQuickFilter( {update: evt} } ) );
至:
this.store.dispatch( actions.setQuickFilter( {update: {id: evt.id, changes: evt} } ) );
现在,我所有订阅的功能都可以使用按钮中的更新值来控制它们自己的 UI 元素。终于!
我决定寻求帮助,但我无法理解 NGRX 实体! (此代码最初由 NX 创建)。
我遵循了 NGRX 实体指南,我也看了很多教程视频,但我仍然无法使 NGRX 实体 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();
您发送的操作不正确。
this.store.dispatch(actions.setQuickFilter(evt));
应该是
this.store.dispatch(actions.setQuickFilter({ update: evt }));
耶!!最后。 这是一个真正的愚蠢错误——因为不理解实体。 大量的反复试验和记录来解决这个问题!
解决方法: 更改组件中的调度调用 from:
this.store.dispatch( actions.setQuickFilter( {update: evt} } ) );
至:
this.store.dispatch( actions.setQuickFilter( {update: {id: evt.id, changes: evt} } ) );
现在,我所有订阅的功能都可以使用按钮中的更新值来控制它们自己的 UI 元素。终于!