且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Mongoose 中保存模型无法保存嵌套组件

更新时间:2023-12-01 22:28:22

要在名为 type 的嵌入对象中定义一个字段,需要使用对象表示法来定义其类型,否则 Mongoose 认为是定义父对象的类型.

To define a field in an embedded object named type, you need to use the object notation to define its type or Mongoose thinks it's defining the type of the parent object.

因此将您的架构更改为:

So change your schema to:

var memberSchema = mongoose.Schema({

    'project'       : { 
        'type'      : Schema.Types.ObjectId, 
        'ref'       : 'Project' 
    },
    'first'         : String,
    'last'          : String,
    'email'         : String,
    'tracker'       : {
        'etag'      : String,
        'id'        : String,
        'photoLink' : String,
        'role'      : String,
        'type'      : {'type': String},   // Here...
    },
    'survey'        : {
        'etag'      : String,
        'id'        : String,
        'photoLink' : String,
        'role'      : String,
        'type'      : {'type': String},   // ...and here
    },
});