angular - 当在后端定义相关id时,如何为实体适配器定义selectId?
问题描述
我正在我的商店中调度 upsert 操作/reducer 以向状态添加新记录,并且有一个效果会调用后端并将记录/文档添加到我的 mongodb 实例。
鉴于此特定模型的唯一合理 id 是在创建文档时由我的后端逻辑定义的,我应该如何在前端的实体实现中定义 selectId?
目前,一个项目被添加到状态,id: undefined
我收到警告:
entity.js:76 @ngrx/entity:传递给
selectId
实现的实体返回未定义。您可能应该提供自己的selectId
实现。
我的适配器定义为:
export const adapter: EntityAdapter<PublishedData> = createEntityAdapter<
PublishedData
>({ selectId: (publishedData: PublishedData) => publishedData.doi });
相关的效果是:
@Effect()
UpsertPublishedData$ = this.actions$.pipe(
ofType<UpsertPublishedData>(PublishedDataActionTypes.UpsertPublishedData),
switchMap(action => this.publishedDataApi.create(action.payload.publishedData)
.pipe(mergeMap((data: PublishedData) => [ new UpsertPublishedData({ publishedData: data }),
this.publishedDataApi.register(data[0].doi)]),
catchError(err => of(new FailedPublishedDataAction(err)))))
);
publishedData.doi
是我需要用来引用实体的违规字段。
解决方案
现在我已经修改了reducer,使其不在id字段(doi)未定义的第一次传递中添加记录。
case PublishedDataActionTypes.UpsertWaitPublishedData: {
if (action.payload.publishedData && action.payload.publishedData.doi) {
return adapter.upsertOne(action.payload.publishedData, state);
}
return state;
}
从这个最初:
case PublishedDataActionTypes.UpsertPublishedData: {
return adapter.upsertOne(action.payload.publishedData, state);
}
这确保状态中的记录与数据库中的记录匹配。这种方法仍然与不需要额外UpsertPublishedDataSuccess
行动的实体方法一致。
推荐阅读
- java - 如何以一种方法处理 2 个数据库?
- python - 类型错误:“在‘str’和‘float’的实例之间不支持”?
- javascript - 将输入文本的值解析为 GTM 变量宏?
- r - 如何使用 WorldClim 数据计算 R 中的热指数
- c - 在 MacOS 上调用 abort() 会导致陷阱,但不会产生核心转储
- c# - 获取不同对象的Json String中的一个对象
- c++ - 为什么没有调用copy assignment方法?
- ubuntu - 无法将 WebP 添加到 ImageMagick
- python - 在python中的图像中的表格上创建边框
- haskell - Haskell:你如何检查 IO 上的运行时类型?