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

Mongoose子文档赋值报错:Cannot set properties of undefined

问题:Mongoose子文档赋值时出现"Cannot set properties of undefined"错误

我是Node.js、Express和MongoDB新手,正在创建REST API处理家庭表单数据,尝试用Mongoose子文档存储父母信息,但无法将数据存入数据库。

模型代码

const fatherSchema = new mongoose.Schema(
    {
        fatherName:{
            type:String,
            require:true,
        },
        fatherProfession:{
            type:String,
            require:true,
        },
        fatherImage:{
            type:String,
            require:true,
        }   
    }
)

const motherSchema = new mongoose.Schema(
    {
        motherName:{
            type:String,
            require:true,
        },
        motherProfession:{
            type:String,
            require:true,
        },
        motherImage:{
            type:String,
            require:true,
        }   
    }
)

const familyDetailsSchema = new mongoose.Schema(
    {
        language:{
            type:String,
            require:true
        },
        father:fatherSchema,  
        mother:motherSchema,
        details:{
            type:String,
            require:true
        },
        user_id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User'
        },
        createdAt: {
            type: Number
        },
        updatedAt: {
            type: Number,
        }
    },
    {
        timestamps:true,
    }
)

路由中的数据添加代码

// ADD INTO FORM
let form = new FamilyDetails(_.pick(req.body,['language','details','user_id']))
form.father.fatherName = req.body.fatherName
// form.father.fatherProfession = req.body.fatherProfession
// form.father.fatherImage = fatherPic
// form.mother.motherImage = motherPic
// form.mother.motherName = req.body.motherName
// form.mother.motherProfession = req.body.motherProfession

错误信息
执行form.father.fatherName = req.body.fatherName时出现错误:"Cannot set properties of undefined (setting 'fatherName')"


解决方案

1. 错误原因

创建FamilyDetails实例时,仅传入了language、details和user_id三个字段,没有初始化father和mother子文档对象,导致form.father为undefined,无法对其属性进行赋值操作。

2. 修复方法

有两种常用的修复方式:

方式一:创建实例时直接传入完整子文档数据

构造包含子文档的完整对象,一次性传入实例:

let form = new FamilyDetails({
  language: req.body.language,
  details: req.body.details,
  user_id: req.body.user_id,
  father: {
    fatherName: req.body.fatherName,
    fatherProfession: req.body.fatherProfession,
    fatherImage: fatherPic
  },
  mother: {
    motherName: req.body.motherName,
    motherProfession: req.body.motherProfession,
    motherImage: motherPic
  }
});
方式二:先初始化子文档对象再赋值

实例创建后,先给father和mother赋值为空对象(或子文档实例),再设置属性:

let form = new FamilyDetails(_.pick(req.body,['language','details','user_id']));

// 初始化father子文档并赋值
form.father = {};
form.father.fatherName = req.body.fatherName;
form.father.fatherProfession = req.body.fatherProfession;
form.father.fatherImage = fatherPic;

// 初始化mother子文档并赋值
form.mother = {};
form.mother.motherName = req.body.motherName;
form.mother.motherProfession = req.body.motherProfession;
form.mother.motherImage = motherPic;

3. 额外提示:修正模型拼写错误

模型中所有字段的require应为required(拼写错误),这会导致字段校验逻辑失效,建议统一修正:

// 示例:修正fatherSchema中的字段
fatherName:{
    type:String,
    required:true, // 将require改为required
},

内容的提问来源于stack exchange,提问作者Adil Ijaz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 22:15:32