You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swagger UI配置报错:TypeError: Cannot read properties of undefined (reading 'schemas')

解决Swagger JSDoc的TypeError: Cannot read properties of undefined (reading 'schemas')问题

核心修复点

你的代码存在两个关键问题引发了该错误:

  • $ref值未加引号:YAML语法中,包含#的字符串必须用引号包裹,否则会被解析为无效语法。需将$ref: #/components/schemas/Accounttypes修改为$ref: '#/components/schemas/Accounttypes'。
  • 确保注释块被完整扫描:检查swagger-jsdoc的配置,确认当前文件路径已被包含,保证components和paths的注释块都能被正确读取。

修正后的代码

/**
 * @swagger
 * components:
 *      schemas:
 *          Accounttypes:
 *              type: string
 */

/**
 * @swagger
 * paths:
 *  /api/accounttypes/:
 *      get:
 *          summary : Some summary
 *          tags:
 *              - Account Types
 *          description: Some description
 *          responses:
 *              '200':
 *                  description: Success Description
 *                  content:
 *                      application/json:
 *                          schema:
 *                              type: array
 *                              items:
 *                                  $ref: '#/components/schemas/Accounttypes'
 */

额外排查方向

  • 升级swagger-jsdoc到最新稳定版:执行npm update swagger-jsdoc,旧版本可能存在语法兼容性问题。
  • 确认OpenAPI版本匹配:在swagger定义中指定openapi: 3.0.0及以上版本,components是OpenAPI 3.x的语法,与2.x版本不兼容。

内容的提问来源于stack exchange,提问作者Quick Hotshot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 05:08:13