Platform: initialState not initialised

Created on 23 May 2018  Â·  7Comments  Â·  Source: ngrx/platform

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request

What is the current behavior?

I have a global reducer defined as:

export const reducer: ActionReducerMap<ApplicationModel> = {
  filter: filterReducer,
  grid: gridReducer,
  analyse: analyseReducer,
};

And an initial state defined as:

export const INITIAL_MODEL: ApplicationModel = {
  filter: FILTER_INITIAL_MODEL,
  grid: GRID_INITIAL_MODEL,
  analyse: ANALYSE_INITIAL_MODEL,
};

And my store as:

@NgModule({
  imports: [
    StoreModule.forRoot(reducer, {
      initialState: INITIAL_MODEL,
      metaReducers: !environment.production ? [storeFreeze] : [],
    }),
    StoreDevtoolsModule.instrument({
      maxAge: 30,
      logOnly: environment.production,
    }),
  ],
})
export class ModelModule {}

And when looking at the chromium redux dev inspector, I see that the initial state is:

{
  filter: {},
  grid: {
    parameters: {
      min: 0.2,
      max: 0.7
    }
  }
}

So basically, half of my state is initialised (or maybe nothing is and for a reason, the grid part of the model is initialised by the reducer before the @ngrx/store/init event arrives…

I have tested injecting the initial state with an exported function. And if I log in this function the value of INITIAL_MODEL, it has non-empty objects for every properties of the initial state.

Expected behavior:

The state should be properly initialised with the value provided to the StoreModule.

Minimal reproduction of the problem with instructions:

The provided plunkr does not work anymore but if there is a new one for the latest version of ngrx, I can reproduce this :)

Version of affected browser(s),operating system(s), npm, node and ngrx:

Happening with ngrx 6.0.0-beta.3 and angular 6.

Most helpful comment

@tdeschryver it's stupid, the reducer of the missing property had no default case for the actions, so it was returning undefined.
And for the empty object, I think it is just store-devtools that hide property with undefined value.

All 7 comments

I don't see immediately what the problem might be.
There is no updated plunkr (I think), but if you want you can use StackBlitz to reproduce the issue.
I've created a ngrx-seed which you can use.

@tdeschryver thanks for the seed.

So… yeah it was my fault, I realised that when trying to reproduce :) thanks again and sorry for the noise

No problem.
What was the problem tho, just out of curiosity 😄

@tdeschryver it's stupid, the reducer of the missing property had no default case for the actions, so it was returning undefined.
And for the empty object, I think it is just store-devtools that hide property with undefined value.

@victornoel you are a life saver. We were having issue where state was undefined and we knew that shouldn't be the case. We double-checked our reducer and were missing the default fall through in the switch statement. We had copied from another project and accidentally left off the default case.

I love Google and Github.

@wesleygrimes glad my mistake and absence of shame to complain could be useful ^^

"... it's stupid, the reducer of the missing property had no default case for the actions, so it was returning undefined."
had the same issue, saved me a lot of headache!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shyamal890 picture shyamal890  Â·  3Comments

axmad22 picture axmad22  Â·  3Comments

RichardMisiak picture RichardMisiak  Â·  3Comments

brandonroberts picture brandonroberts  Â·  3Comments

hccampos picture hccampos  Â·  3Comments