Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this schema where discriminator is defined by the type field.

const FilledSchema = new mongoose.Schema({
    template:{ type: mongoose.SchemaTypes.ObjectId, ref: 'Template'},
    user_id: mongoose.SchemaTypes.ObjectId,
    type: String,
}, {discriminatorKey: 'type'});

The type field isn't ready until this function is completed:

FilledSchema.pre('validate', function(next) {
    var data= this;
    this.populate('template', function(err, res) {
        if (err) next(err);
        if (res.template === null) next('Template not found.');
        data.type = res.template_id.type;
        next();
    });
})

After doing some tests I noticed that the discriminatorKey gets a value (null) before the pre middleware is run. How can I set the value to the discriminatorKey after I populate the "template" field?

question from:https://stackoverflow.com/questions/65943671/mongoose-run-function-before-setting-discriminatorkey

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
62 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...