michaelkrone / ngrx-normalizr

Managing normalized state in ngrx applications - transparently
https://michaelkrone.github.io/ngrx-normalizr/
MIT License
50 stars 17 forks source link

How do you link the feature selector with the schema selector? #38

Open irinelpascu opened 6 years ago

irinelpascu commented 6 years ago

I cannot understand how I can create a selector to get the feature entities. Trying to get them using the schemaSelectors.getEntities selector throws an error: ERROR TypeError: Cannot read property 'entities' of undefined at normalize.js:99 at store.js:577 at memoized (store.js:519) at defaultStateFn (store.js:546) at store.js:585 at memoized (store.js:519) at store.js:545 at Array.map () at defaultStateFn (store.js:545) at store.js:585

import {
  ActionReducerMap,
  createFeatureSelector
} from '@ngrx/store';
import {
  NormalizedState,
  normalized,
  createSchemaSelectors
} from 'ngrx-normalizr';
import { Section } from '../../models/section.model';
import { sectionSchema } from '../../schemas/section.schema';

export interface ReportingState extends NormalizedState {
}

export const reducers: ActionReducerMap<NormalizedState> = {
  normalized
};

export const getReportingState = createFeatureSelector<ReportingState>(
  'reporting'
);

export const getReportingSchemaSelectors = createSchemaSelectors<Section>(sectionSchema);
export const getSectionEntities = createSelector(
  fromFeature.getReportingState,
  fromFeature.getReportingSchemaSelectors.getEntities
);
herrpatrickmueller commented 5 years ago

I don't understand this either. Have you found a solution yet?

zahnrodolfo commented 5 years ago

I was able to do it. In my application when we started we had the state without the normalizr and now we are adding features that use the normalizr.

To make the selector work I did the following.

const myFeatureSchemaSelector = createSchemaSelector(Schema);

export const featureState = createFeatureSelector(State)('yourFeatureStateName');

export const getMyFeatureStateDenormalized = createSelector( featureState, myFeatureSchemaSelector.getEntities );

I hope it helps @irinelpascu @parallactic-zurich