I have an array of TripEvents in my store:
export interface TripEvent {
id: string;
title: string;
address: string;
image: string;
prevSibling: {
title: string,
};
}
I also have an effect triggered whenever an item is added to an array in my store:
findSiblings$ = createEffect(() => this.actions$.pipe(
ofType(TripsActionTypes.AddEvent),
concatMap(() => this.tripsService.findSiblings().pipe(
map(events => SiblingsFound(events))
))
));
The findSiblings()
function needs to do some complicated work eventually so I have put it in a service, but for now I'm just trying to update each event in the array with the name of its sibling:
findSiblings() {
this.store.select(getEvents).pipe(take(1)).subscribe(events => {
this.events = events;
});
const updatedEvents = this.events.map((currentEvent, index, array) => {
if (index > 0) {
const prevEvent = array[index - 1];
currentEvent.prevSibling.title = prevEvent.title;
}
return currentEvent;
});
return of(updatedEvents);
}
This gives me the error TypeError: Cannot assign to read only property 'title' of object '[object Object]'
. I understand maybe this is because I am trying to update items in the store directly? But no matter how I try to clone or copy the array or elements I get the same error, so what is the general approach to a) updating the store via a service, or b) returned modified items from the store for the action/reducer to update (e.g. via SiblingsFound action above).