How to set enum default value in mongoose sub doc?

I am using mongodb along with mongoose js in my node js application. I created a mongoose document schema called "CompanySchema" which uses TeamSchema (another mongoose document schema) as a supporting document. Inside this TeamSchema, it has an array defined as employees, which uses EmployeeSchema (another mongoose document) as a subdocument. So my question is when I try to save the CompanySchema document, the default value for the status of the unsatisfied requirement is not set. Can you guys explain to me what I'm doing wrong here?

export var EmployeeSchema = new Schema({
 id: {
   type: String
 },
 requirement: {
   type: {
     status: {
       type: String,
       enum: ['met' 'unmet'],
       default : 'unmet'
     }
   },
   default: null
 },
});

export var TeamSchema = mongoose.model<TeamModel>("Team", new mongoose.Schema({
 id: {
   type: String,
 },
 name: {
   type: String
 },
 employees: [EmployeeSchema]
}));

export var CompanySchema = mongoose.model<CompanyModel>("Company", new mongoose.Schema({
 id: {
   type: String
 },
 team: TeamSchema.schema,
}));
+6
1

, .

-, Mongoose type.

, "type" , mongoose .

Mongoose doc: typeKey

-, null, , type . type requirement_type , :

TypeError: Invalid value for schema path `requirement.default`

, .

SchemaType # ()
SchemaType.

Mongoose doc: SchemaType-default

, null , , Mixed type:

requirement: {
   type: {},
   requirement_type: {
     status: {
       type: String,
       enum: ['met', 'unmet'],
       default : 'unmet'
     }
   },
   default: null
 }
 // => { requirement_type: null }

, :

requirement: {
   requirement_type: {
     status: {
       type: String,
       enum: ['met', 'unmet'],
       default : 'unmet'
     }
   }
 }
// => { requirement_type: { status: 'unmet' } }

. .

+1

Source: https://habr.com/ru/post/1016822/


All Articles