tomastrajan / angular-ngrx-material-starter

Angular, NgRx, Angular CLI & Angular Material Starter Project
https://tomastrajan.github.io/angular-ngrx-material-starter
MIT License
2.83k stars 917 forks source link

Error on refresh of CRUD example page if navigate to Todo/Stocks 1st. Must clear localstorage/data. "Cannot read property 'entities' of undefined at selectEntities" #345

Closed chauey closed 5 years ago

chauey commented 6 years ago

Minimal reproduction of the bug with instructions:

Expected behavior:

Other information:

Perhaps related to rehydrating app state from localstorage but not having code to handle missing collection data

I would be willing to submit a PR to fix this issue:

[ ] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No

tomastrajan commented 6 years ago

@chauey thank you very much for your feedback we will have a look into this!

timdeschryver commented 6 years ago

The problem here is comes from initStateFromLocalStorage. The example state is getting partially loaded with {todos: {...}} and we're overriding the example state with this state from the local storage. Because we don't have a local storage state from our CRUD reducer, this CRUD state just disappears.

To solve this we'll have to spread the example state, instead of overriding the example state as a whole. In other words the following line is wrong:

return { ...newState, ...LocalStorageService.loadInitialState() };

If you want you can give this a shot @chauey , otherwise I'll submit a PR.

timdeschryver commented 5 years ago

This is solved.