I am trying to retrieve the state for my application using NgRx but I cannot get it to work. I get the error:
ERROR TypeError: Cannot read properties of undefined (reading 'machines')
the error points to here in the selector below. I can add the ? operator, that causes the issue to go away but nothing render on the front end when i use the async pipe machine.selector.ts
export const selectMachines = (state: AppState) => state.machineState;
export const selectAllMachines = createSelector(
selectMachines,
(state) => state.machines
);
how I select the machines in my component
public allDevices$ = this.store.select(selectAllMachines);
machine reducer and state
export interface AppState {
machineState: MachinesState;
}
export interface MachinesState {
machines: Machine[];
}
const initialState: MachinesState = {
machines: [
{ id: '1', name: 'WASHER #1', status: Status.Available, type: 'Washer' },
{
id: '2',
name: 'WASHER #2',
status: Status.UnAvailable,
type: 'Washer',
user: 'Flat 4',
},
{ id: '3', name: 'DRYER #1', status: Status.Available, type: 'Dryer' },
{
id: '4',
name: 'DRYER #2',
status: Status.UnAvailable,
type: 'Dryer',
user: 'Flat 1',
},
],
};
export const machineReducer = createReducer(
initialState,
on(updateMachine, (state, { machine }) => {
const index = state.machines.findIndex((d) => d.id === machine.id);
state.machines[index] = machine;
return state;
}),
on(getMachines, (state) => {
return state;
})
);