why am getting this below error and how to fix this .
The entity passed to the selectId implementation returned undefined. You should probably provide your own selectId implementation. The entity that was passed:
Spinners or other loading indicators are frequently wired up to isLoading or pending states in NgRx. Some claim that it shouldn’t be stored as a separate property and it is a derived state, which is determined by the presence of the data itself — if it’s not there it might be loading.
So whether to store the error locally or in the NgRx Store depends on your use case, and the latter might be a safer choice. Spinners or other loading indicators are frequently wired up to isLoading or pending states in NgRx.
One thing that took a while for us to get our heads around is NgRx Effects. It’s how you deal with asynchronous actions in Redux state; in short, it gives you a hook into your Redux store to say “every time an event is dispatched, after it’s been reduced, let me know.”
If we do that, the Observable that NgRx Effects subscribes to is going to be replaced by the Observable emitted in the catchError method, and then all future values emitted from action$ are going to be ignored (the new Observable in the error handler is not subscribed to them).
Probably you are passing an entity which has no id property. So you need to override the selectId
method in the EntityAdapter
creation.
export const adapter: EntityAdapter<YourInterface> =
createEntityAdapter<YourInterface>({
selectId: nameofclass => nameofclass.yourID
});
I have fixed this issue by specifying metadata for several entities at the same time in an EntityMetadataMap:
// entity-store.module.ts
...
export function selectEventId(event: Event): number {
return event.eventId;
}
export const entityMetadata: EntityMetadataMap = {
User: {},
Event: {
selectId: selectEventId
}
};
...
This error is typically linked to missing - Entity primary key:
Every entity type must have a primary key whose value is an integer or a string. The NgRx Data library assumes that the entity has an "id" property, whose value is the primary key.
Not every entity will have a primary key property named "id". For some entities, the primary key could have any name or could be a combined value of two or more properties. In these cases, you always specify the primary key for that entity using the function:
selectId
This function returns the primary key value of the specified field/property of that entity.
For example:
Entity Villain does not have a primary key named "id", but it is named "key". For this entity, the selectId function is this:
selectId: (villain: Villain) => villain.key;
And the full declaration of the entity metadata could be something like:
File: ../entity-store.module.ts
const entityMetadata: EntityMetadataMap = {
...
Villain: {
// We MUST specify a primary key / id field for each entity
// (if it does not have its own prop/field named: 'id') !!!
selectId: (villain: Villain) => villain.key, // <---
// optional setting - filter
filterFn: filterForVillains,
// optional setting - sort order
sortComparer: sortVillains
},
Hero { ... },
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With