您可以将数据传递给
Model.save()呼叫,然后将其传递给中间件。
// in your route/controllervar item = new Item();item.save(req, function() { });// in your modelitem.pre('save', function (next, req, callback) { console.log(req); next(callback);});
不幸的是,今天这不适用于嵌入式模式(请参阅https://github.com/LearnBoost/mongoose/issues/838)。一种解决方法是将属性附加到父项,然后在嵌入式文档中访问它:
a = new newModel;a._saveArg = 'hack';embedded.pre('save', function (next) { console.log(this.parent._saveArg); next();})
如果您确实需要此功能,建议您重新打开我上面链接的问题。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)