Importing a model schema as a subdocument

1.3k views Asked by At

I have 2 separate models:

models/day.js:

var mongoose = require ('mongoose');
var Schema = mongoose.Schema;
var shiftSchema = require('./shift').shiftSchema;

var daySchema = new Schema({
    date: {
        type: String,
        required: true
    },
    shifts: [shiftSchema]
});
module.exports = {
     model: mongoose.model('Day', daySchema)
};

and

models/shift.js:

var mongoose = require ('mongoose');
var Schema = mongoose.Schema;

var shift_status_enum = {
  values: ['open', 'closed'],
  message: '`{VALUE}` is not a valid shift status.'
};
var shiftSchema = new Schema({
    start: {
        type: Number,
        required: true
    },
    end: {
        type: Number,
        required: true
    },
    status: {
        type: String,
        enum: shift_status_enum,
        required: true,
        default: 'open'
    }
});
shiftSchema.pre('save', function(next) {
    if (this.start >= this.end)
        return next(Error('error: Shift end must be greater than shift start.'));
    if(this.interval > this.end - this.start)
        return next(Error('error: Shift interval cannot be longer than the shift.'));
    else
        next();
});
module.exports = {
     model: mongoose.model('Shift', shiftSchema)
};

and I'm trying to embed an array of shifts into day as shown above. However, when I try referencing the shiftSchema in the daySchema, I get the error:

TypeError: Invalid value for schema Array pathshifts``. However, when I tried copying the shiftSchema into the same file, it worked. Is it possible to reference the child schema (with all its validations) without having it in the same file as the parent schema or do they have to be in the same file?

1

There are 1 answers

1
Simran On BEST ANSWER

Basically you are merging the concept of sub Document and Document. In above given models you are creating two documents and then inserting one document into another.

How can we say that you are exporting document into document not sub-document ?

Ans:Exporting this line of code mongoose.model('Shift', shiftSchema) make it complete document

If you only export module.exports = shiftSchema then you can achieve what you r trying to do.

so,In my opinion you can restructure your daySchema like:

var daySchema = new Schema({
    date: {
        type: String,
        required: true
    },
    shifts: [Schema.Types.ObjectId]
});

shifts will contain array of objectIds of shifts documents. I personally feel this approach is better but if u want your code to run then go for this :

var mongoose = require ('mongoose');
var Schema = mongoose.Schema;
var shiftSchema = require('./shift');

var daySchema = new Schema({
    date: {
        type: String,
        required: true
    },
    shifts: [shiftSchema]
});
module.exports = {
     model: mongoose.model('Day', daySchema)
};

models/shift.js:

    var mongoose = require ('mongoose');
    var Schema = mongoose.Schema;

    var shift_status_enum = {
      values: ['open', 'closed'],
      message: '`{VALUE}` is not a valid shift status.'
    };
    var shiftSchema = new Schema({
        start: {
            type: Number,
            required: true
        },
        end: {
            type: Number,
            required: true
        },
        status: {
            type: String,
            enum: shift_status_enum,
            required: true,
            default: 'open'
        }
    });
module.exports = shiftSchema