mongo全局插件不可用? #2080
Unanswered
feng23030206
asked this question in
Q&A
mongo全局插件不可用?
#2080
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
项目是基于typegoose开发的,我在middware中定义了mongo的全局plugin,用于为每个schema都添加时间戳,但是不起作用,在2.x版本是好使的,升级后就不好使了,请大神指教。下面为具体代码
import * as mongoose from 'mongoose';
....
@provide()
export class orm implements IWebMiddleware {
@Inject()
conn: MongooseConnectionService;
resolve() {
return async (ctx: Context, next: IMidwayWebNext) => {
console.log(ctx.originalUrl);
console.log('全局中间件调用-controller之前');
mongoose.plugin(PreOption);
// this.conn.plugin(PreOption);
}
PreOption(schema: mongoose.Schema) {
schema.add({ ct: Date, ut: Date });
schema.pre('save', function (next) {
this.set('ct', new Date());
this.set('ut', new Date());
next();
});
schema.pre('updateOne', function (next) {
this.updateOne({ $set: { ut: new Date() } });
next();
});
schema.pre('insertMany', function (next) {
this.set('ct', new Date());
this.set('ut', new Date());
next();
});
schema.pre('findOneAndUpdate', function (next) {
this.update({ $set: { ut: new Date() } });
next();
});
}
}
Beta Was this translation helpful? Give feedback.
All reactions