2017-02-27 23 views
2

我正在使用貓鼬並試圖級聯刪除,但不幸的是我的預刪除中間件不是由於某種原因而燒製的。預刪除中間件未燒製

var presentationSchema = new Schema({ 
    id: Number, 
    title: String, 
    pdfURL: String, 
    created_at: Date, 
    updated_at: Date, 
    slides: [{ 
     type: Schema.Types.ObjectId, 
     ref: 'Slide' 
    }] 
    }); 

    presentationSchema.pre('remove', function(next) { 
    console.log("delete slides" + this._id); 
     next(); 
    }); 

    // the schema is useless so far 
    // we need to create a model using it 
    var Presentation = mongoose.model('Presentation', presentationSchema); 


    // make this available to our users in our Node applications 
    module.exports = Presentation; 

回答

3

也許你會受到this mongoose "feature"上癮:

注:對於刪除(沒有查詢鉤),只爲文件。如果 設置了「刪除」鉤子,當您調用myDoc.remove()時,它將被觸發,而當您調用MyModel.remove()時,它將被觸發。注意:create()函數觸發save()鉤子。

,當你調用myPres.remove()pre('remove',...)中間件將被解僱不是要求從模型中刪除的功能就像Presentation.remove()

+0

感謝,這是正是我的問題,當 – otusweb