2017-08-30 101 views
0

我試圖刪除MongoDB中引用的文件時,公司是deleting.But後,我執行以下只刪除了公司,而不是執行中間件體貓鼬預功能不會對中間件工作刪除

const removedCompany = await CompanyModel.findOne({ _id: id }).remove(); 

架構文件內

CompanySchema.pre('remove', (next) => { 
    // 'this' is the company being removed. Provide callbacks here if you want 
    // to be notified of the calls' result. 
    UserCompany.remove({ companyId: this._id }).exec(); 
    next(); 
}); 

回答

0

the documentation

注意:沒有查詢鉤remove(),只針對文件。如果您設置了「刪除」掛鉤,則在致電myDoc.remove()時將被觸發,而不是在致電MyModel.remove()時觸發。

如果您重寫查詢以使用findOneAndRemove,則可以爲其添加中間件/掛鉤。

也考慮到Shubham關於箭頭函數表達式的回答。