ngrx-normalizr icon indicating copy to clipboard operation
ngrx-normalizr copied to clipboard

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

Open irinelpascu opened this issue 6 years ago • 2 comments

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
);

irinelpascu avatar Aug 22 '18 11:08 irinelpascu

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

herrpatrickmueller avatar Nov 19 '18 15:11 herrpatrickmueller

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<Model>(Schema);

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

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

I hope it helps @irinelpascu @parallactic-zurich

zahnrodolfo avatar Dec 18 '18 20:12 zahnrodolfo