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

Express+Mongoose运行报错:Invalid schema configuration排查求助

Mongoose Schema配置错误:status.default类型无效问题

问题详情

基于Express+Mongoose开发的应用运行报错,涉及的Schema字段代码片段如下:

status: {
  enum: ["active", "inactive"],
  default: "active"
}

控制台抛出错误:

throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
^
TypeError: Invalid schema configuration: `Active` is not a valid type at path `status.default`.

问题原因

Mongoose的Schema字段必须明确声明数据类型(type),否则会将enum、default等配置项错误识别为字段类型。这里由于未指定type,Mongoose误将默认值"active"首字母大写后当作类型Active查找,导致抛出类型无效的错误。

修复方案

为status字段添加type: String声明,修正后的代码:

status: {
  type: String,
  enum: ["active", "inactive"],
  default: "active"
}

内容的提问来源于stack exchange,提问作者Tafsir Chowdhury

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 18:01:16