核心服务方法使用文档服务 API
页面摘要: 在 Strapi 5 中,核心服务方法使用文档服务 API 而非实体服务 API,其中
findOne、update和delete等方法接收documentId而非entityId。
在 Strapi 5 中,核心服务方法使用文档服务 API 而非实体服务 API。
本页面是破坏性变更数据库的一部分,提供有关该破坏性变更的信息,以及从 Strapi v4 迁移到 Strapi 5 的附加说明。
破坏性变更说明
在 Strapi v4 中
核心控制器和 createCoreService 工厂默认使用实体服务 API。
例如 find、update 和 delete 等方法接收 entityId。
在 Strapi 5 中
核心控制器和 createCoreService 工厂使用 文档服务 API。
例如 find、update 和 delete 等方法接收 documentId。
迁移
本节汇总了关于该破坏性变更的有用说明和操作步骤。
说明
一些核心方法会调用 super.find(ctx),该方法在 Strapi v4 中内部调用实体服务方法,而在 Strapi 5 中则调用文档服务 API 方法。这可能导致部分查询不再生效,或返回的结果与预期略有不同。
以下示例展示了代码应如何更新:
在 Strapi v4 中:
const { createCoreService } = require('@strapi/strapi').factories;
module.exports = createCoreService('api::address.address', {
findOne(entityId, params) {
// customization
super.findOne(entityId, params);
// 或展示更多上下文
strapi.entityService.findOne(uid, entityId, params);
},
update(entityId, params) {
// customization
super.update(entityId, params);
},
delete(entityId, params) {
// customization
super.delete(entityId, params)
}
});
在 Strapi 5 中:
const { createCoreService } = require('@strapi/strapi').factories;
module.exports = createCoreService('api::address.address', {
findOne(documentId, params) {
// customization
super.findOne(documentId, params);
// 或展示更多上下文
strapi.documents(uid).findOne(documentId, params);
},
update(documentId, params) {
// customization
super.update(documentId, params);
},
delete(documentId, params) {
// customization
super.delete(documentId, params)
}
});
手动操作步骤
要更新你的自定义代码:
- 查找所有带有自定义逻辑的
createCoreService调用。 - 对于任何为集合类型扩展核心方法的
findOne、delete、update函数,请按照 说明 进行更新。
此外,请参阅 实体服务 API 到文档服务 API 迁移 文档。