2016-08-03 38 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