2016-08-03 36 views
0

我想觸發remove操作到的貓鼬pre.save掛鉤中的ModelB操作。使用貓鼬在不同的模型鉤子內執行模型操作

基本上任何ModelA更新任何時候,我需要刪除該ModelB集合:

這是我試過了,我沒有得到錯誤,但操作永遠不會結束:

const mongoose = require('mongoose'); 
const Schema = mongoose.Schema; 
const ObjectId = Schema.Types.ObjectId; 

const permissionSetSchema = require('./permission-set'); 
const PermissionSet  = mongoose.model('PermissionSet', permissionSetSchema); 

const roleSchema = new Schema({ 
    name  : { type: String, required: true, unique: true, maxLength: 140 }, 
    description: { type: String, maxLength: 300 }, 
}); 

roleSchema.post('update', (next, done) => { 
    PermissionSet.remove({}, err => { 
    if (err) { next(err); } 

    next(); 
    }); 
}); 

回答

1

的第一個參數是文件。第二個是下一個回調。 應該是:

roleSchema.post('update', (doc, next) => { 
 
    PermissionSet.remove({}, err => { 
 
    if (err) { next(err); } 
 

 
    next(); 
 
    }); 
 
});

http://mongoosejs.com/docs/middleware.html

+0

哦愚蠢的錯誤,謝謝! – ianaya89