7

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:

Amit Dubey
  • 71
  • 1
  • 3

3 Answers3

13

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
  });
adiga
  • 34,372
  • 9
  • 61
  • 83
1

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

...
Milena
  • 886
  • 1
  • 7
  • 18
1

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 { ... },

    ....

}
Felix
  • 1,662
  • 2
  • 18
  • 37