I have integrated NGRX effects into my Angular application and encountered the following error. I'm uncertain if I am using the selector correctly in my component to query the store?
core.js:6162 ERROR TypeError: Cannot read property 'map' of undefined
at ngrx-entity.js:21
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:1078
at Array.map (<anonymous>)
Component
ngOnInit() {
this.initializeForModel();
//this.users$ = this.store.select(getAllUsers);
this.store.select(getAllUsers).pipe().subscribe((response:any)=> {
this.listOfUser = response.users;
this.userMange = this._createGroup();
});
Selector
export const userFeatureSelector = createFeatureSelector<UserState>('users');
export const getAllUsers = createSelector(
userFeatureSelector,
selectAll,
selectIds
);
Service
constructor(private http: HttpClient) { }
getAllUsers(): Observable<any> {
return this.http.get("assets/user.json");
}
}
Reducer
import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
export interface UserState extends EntityState<User> {
usersLoaded: boolean;
}
export const adapter: EntityAdapter<User> = createEntityAdapter<User>();
export const initialState = adapter.getInitialState({
usersLoaded: false,
users: [
],
});
export interface UserState {
users: User[];
}
export const userReducer = createReducer(
initialState,
on(UserActions.loadUsers, (state, { users }) => {
return adapter.setAll(users, state);
}),
Action
export const loadUsers = createAction(
'[Users List] Load Users via Service',
props<{ users: User[] }>()
);
Effects
loadUsers$ = createEffect(() =>
this.actions$.pipe(
ofType(UserActions.loadUsers),
mergeMap(() => this.UserService.getAllUsers()
.pipe(
map(users => ({ type: UserActions.loadUsers.type, payload: users }))
// map(users => { {console.log(users)} return UserActions.usersLoaded({users})})()
))
)
);