Mongoose sub document pre remove middleware not called

前端 未结 1 1174
臣服心动
臣服心动 2021-01-27 21:55

I would prevent any sub document from being removed, thus I added an error to the pre(\'remove\') middleware of each sub document Schema.

When calling the .remove() func

相关标签:
1条回答
  • 2021-01-27 22:20

    After a little bit of researches, I found this:

    A workaround can be to hook an event to the whole array of Sub Documents, and to have a copy of the previous array of data.

    This is a complete working example on how to be sure an array element hasn't been deleted or pulled out. To check for modifications, you'll need further modifications.

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;
    
    var ResourceSchema = new Schema({
        activation_dates: [subDateSchema]
    });
    
    // This virtual permits to store the original array accross the middlewares
    ResourceSchema.virtual("original").set(function(item){
        this._original = item;
    }).get(function(){
        return this._original;
    });
    
    // This middleware checks for a previous version of the "Resource"
    ResourceSchema.pre("validate", function(next){
        var self = this;
        mongoose.model("Resource").findById(this._id, function(err, doc){
            if(err) throw err;
            self.original = doc;
            next();
        });
    });
    
    // This validation block checks for any modification of the array of sub documents
    ResourceSchema.path("activation_dates").validate(function(value){
        var j;
        if(this.original){
            // if the new array is smaller than the original, no need to go further
            if(this.original.activation_dates.length > value.length){
                return false;
            }
    
            for(var i=0; i < this.original.activation_dates.length; i++){
                j=0;
                // if the array element has been deleted but not pulled out, we need to check it
                if(typeof value[j] == "undefined" || typeof value[j]._id == "undefined"){
                    return false;
                }
    
                while(value.length > j && this.original.activation_dates[i]._id.toString() != value[j]._id.toString()){
                    j++;
                }
                if(j == value.length){
                    return false;
                }
            }
        }
        return true;
    }, "You deleted at least one element of the array");
    
    var Resource = mongoose.model('Resource', ResourceSchema);
    
    var newresource = new Resource({
        activation_dates: [{
            date_add: Date.now()
        }]
    });
    
    newresource.save(function(err){
        if(err) throw err;
    
        newresource.activation_dates.splice(0, 1);
        // OR:
        //delete newresource.activation_dates[0];
        // this line is essential in the case you *delete* but not pull out
        newresource.markModified('activation_dates');
    
        newresource.save(function(err){
            if(err) throw err;
        });
    });
    

    Unfortunately I couldn't find another solution than doing a loop over all elements and retrieving the original document.

    0 讨论(0)
提交回复
热议问题