Strapi v3迁移至v4时出现Cannot read property 'kind' of undefined错误求助
Strapi v3迁移至v4时出现"TypeError: Cannot read property 'kind' of undefined"的解决方法
我正在本地环境用本地数据库将Strapi v3应用迁移至v4,已按v4文档修改配置文件,用codemods调整目录结构并更新routes、controllers和services,但运行应用时触发TypeError: Cannot read property 'kind' of undefined,导致构建失败。相关文件代码如下:
路由文件(route)
'use strict'; /** * blog router */ const { createCoreRouter } = require('@strapi/strapi').factories; module.exports = createCoreRouter('api::blog.blog');
控制器文件(Controller)
'use strict'; /** * blog controller */ const { createCoreController } = require('@strapi/strapi').factories; module.exports = createCoreController('api::blog.blog');
服务文件(service)
'use strict'; /** * blog service */ const { createCoreService } = require('@strapi/strapi').factories; module.exports = createCoreService('api::blog.blog');
模型Schema文件(schema.json)
{ "kind": "collectionType", "collectionName": "blogs", "info": { "singularName": "blog", "pluralName": "blogs", "displayName": "Blog", "name": "blog" }, "options": { "increments": true, "timestamps": true, "draftAndPublish": true }, "pluginOptions": {}, "attributes": { "insightImage": { "type": "media", "allowedTypes": [ "images", "files", "videos" ], "multiple": false, "required": false }, "insightTitle": { "type": "string" }, "insightDescription": { "type": "text" }, "insightContent": { "type": "richtext" }, "slug": { "type": "string", "unique": true }, "categories": { "type": "relation", "relation": "manyToMany", "target": "api::category.category", "mappedBy": "blogs" }, "insightDate": { "type": "date" }, "views": { "type": "integer", "default": 0 }, "featured": { "type": "boolean" }, "frontImage": { "type": "boolean" }, "insightsMetaData": { "type": "component", "repeatable": false, "component": "meta.metadata" }, "industry": { "type": "relation", "relation": "manyToOne", "target": "api::industry.industry", "inversedBy": "blogs" }, "solution": { "type": "relation", "relation": "manyToOne", "target": "api::solution.solution", "inversedBy": "blogs" }, "type": { "type": "relation", "relation": "manyToOne", "target": "api::type.type", "inversedBy": "blogs" }, "featured_topics": { "type": "relation", "relation": "manyToMany", "target": "api::featured-topic.featured-topic", "inversedBy": "blogs" }, "formTitle": { "type": "string" }, "formId": { "type": "string" }, "author": { "type": "relation", "relation": "manyToOne", "target": "api::author.author", "inversedBy": "blogs" }, "enableGated": { "type": "boolean" }, "insightBannerImage": { "type": "media", "allowedTypes": [ "images", "files", "videos" ], "multiple": false, "required": false }, "breakTitleText": { "type": "boolean", "default": false } } }
解决方法
检查关联模型的完整性
这个错误大多是因为Blog模型关联的其他模型(比如Category、Industry、Solution等)没有完成v4迁移。确保所有关联模型的schema.json都存在,且包含kind字段(值为collectionType或singleType),结构完整无缺失。确认组件存在且结构正确
Blog里用到了meta.metadata组件,检查该组件是否在src/components/meta/metadata.json路径下,且组件的schema没有缺失关键字段。清理缓存后重新构建
执行命令清理缓存和构建文件,再重启服务:rm -rf .cache build npm run build npm run develop验证模型目录结构
确保Blog模型的目录是src/api/blog/content-types/blog/schema.json,路径和文件名没有拼写错误,尤其是content-types这个目录不能写错。检查数据库迁移状态
确认已经执行过数据库迁移命令(npm run strapi migrate),如果是直接复用v3的数据库,检查关联表数据是否正常,必要时备份数据后重新执行迁移。
内容的提问来源于stack exchange,提问作者Hussain Shaikh
相关产品推荐
相关产品推荐

