如何在mongoose中将空数组保存为嵌套对象的属性?

时间:2017-09-19 10:35:33

标签: arrays node.js mongoose

我有以下代码:

var lesson = new Lesson({
                classroomId: req.params.classroomId,
                name: req.body.name,
                startDate: startDate1,
                endDate: endDate1,
                teacher: {
                    _id: classroom.teacher._id,
                    attendance: [],
                },
                students: [],
            });

lesson.save();

当我检查后端时,教师密钥只有_id属性,并且没有保存考勤属性。我怀疑这是因为它是一个空数组。如何保存这样的空数组呢?

1 个答案:

答案 0 :(得分:0)

可能是因为未正确定义课程架构。你尝试过设置......

mongoose.Schema({
    .....
    teacher: {
        ...
        attendance: { type: Array, default: [] }
    }
    ...
});