Skip to main content

当填充时,空的 morphMany 关系会返回 [] 而不是 null

🌐 Empty morphMany relations return [] instead of null when populated

Page summary:

在 Strapi 5 中,填充一个空的 morphMany 关系(包括像图库这样的 type: 'media', multiple: true 字段)会返回 [] 而不是 null。更新检查 field === null 的客户端代码,把 [] 当作空状态处理。

🌐 In Strapi 5, populating an empty morphMany relation (including type: 'media', multiple: true fields such as a gallery) returns [] instead of null. Update client code that checks field === null to treat [] as the empty state.

在 Strapi 5 中,空的 morphMany 关系和 type: 'media', multiple: true 字段(比如图库)在被填充时现在会序列化为空数组,这与 oneToMany 和 manyToMany 关系保持一致。之前,当没有相关条目时,这些字段会返回 null。

🌐 In Strapi 5, empty morphMany relations and type: 'media', multiple: true fields (such as a gallery) now serialize as an empty array when populated, consistent with oneToMany and manyToMany relations. Previously, these fields returned null when no related entries existed.

此页面是重大更改数据库的一部分,提供关于重大更改的信息以及从 Strapi v4 迁移到 Strapi 5 的附加说明。

🌐 This page is part of the breaking changes database and provides information about the breaking change and additional instructions to migrate from Strapi v4 to Strapi 5.

 Is this breaking change affecting plugins?Yes
 Is this breaking change automatically handled by a codemod?No

重大变更描述​

🌐 Breaking change description

在 Strapi v4 中

向空的 morphMany 关系或多媒体字段填充内容会返回 null:

🌐 Populating an empty morphMany relation or a multiple media field returned null:

{
"gallery": null
}

在 Strapi 5 中

填充一个空的 morphMany 关系或一个多媒体字段会返回一个空数组:

🌐 Populating an empty morphMany relation or a multiple media field returns an empty array:

{
"gallery": []
}

迁移​

🌐 Migration

本节重新组合了有关引入的重大更改的有用说明和程序。

🌐 This section regroups useful notes and procedures about the introduced breaking change.

注意​

🌐 Notes

  • 此更改适用于所有 morphMany 关系类型,包括 type: 'media', multiple: true 字段(例如图库字段)。
  • 这个改变是无条件的,不受 api.documents.strictRelations 设置控制。
  • 查看 文档服务 API:填充字段 和 REST API:填充与字段选择 以获取关于填充关系的相关信息。

手动操作​

🌐 Manual procedure

检查客户端代码、Webhook 和使用已填充的 morphMany 或多个媒体字段的集成。

🌐 Check client code, webhooks, and integrations that consume populated morphMany or multiple media fields.

  1. 在你的代码库中搜索那些基于 field === null 分支的代码,或把 null 当作已填充 morphMany 或多个媒体字段的“无值”处理的代码。
  2. 用数组检查替换 null 检查,例如使用 !field?.length。

之前

if (entry.gallery === null) {
// handle empty gallery
}

之后

if (!entry.gallery?.length) {
// handle empty gallery
}