2016-08-03 8 views
0

remove操作を、pre.saveフック内のModelBModelAからトリガーしたいとします。モンゴースを使用して異なるモデルのフック内でモデル操作を実行

基本的には任意の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

最初の引数はドキュメントです。 2番目は次のコールバックです。 は次のようになります。

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

 
    next(); 
 
    }); 
 
});

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

+0

ああ愚かな間違い、ありがとう! – ianaya89

関連する問題