7

为什么会出现以下错误以及如何解决此问题。

传递给 selectId 实现的实体返回未定义。您可能应该提供自己的 selectId 实现。通过的实体:

4

3 回答 3

12

可能您正在传递一个没有 id 属性的实体。因此,您需要在创建中覆盖该selectId方法。EntityAdapter

export const adapter: EntityAdapter<YourInterface> =
  createEntityAdapter<YourInterface>({
    selectId: nameofclass => nameofclass.yourID
  });
于 2019-06-06T08:46:47.467 回答
1

我已通过在 EntityMetadataMap 中同时为多个实体指定元数据来解决此问题:

// entity-store.module.ts

...

export function selectEventId(event: Event): number {
  return event.eventId;
}

export const entityMetadata: EntityMetadataMap = {
  User: {},
  Event: {
    selectId: selectEventId
  }
};

...
于 2020-01-04T08:25:41.240 回答
1

此错误通常与缺少 -实体主键有关:

每个实体类型都必须有一个主键,其值为整数或字符串。NgRx 数据库假定实体具有“id”属性,其值为主键。

并非每个实体都有一个名为“id”的主键属性。对于某些实体,主键可以具有任何名称,也可以是两个或多个属性的组合值。在这些情况下,您始终使用以下函数指定该实体的主键:

选择标识

此函数返回该实体的指定字段/属性的主键值。

例如:

Entity Villain 没有名为“id”的主键,但它被命名为“key”。对于这个实体,selectId函数是这样的:

selectId: (villain: Villain) => villain.key;

实体元数据的完整声明可能类似于:

文件:../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 { ... },

    ....

}
于 2020-06-08T17:04:42.950 回答