我喜欢将其转换为函数而不是箭头函数,以用作我的猫鼬模式中的中间件。任何人都可以建议如何做到这一点?谢谢
module.exports = (field: string) => function (next: any) {
this.populate(field);
next();
};
这与 Stackoverflow 上的其他箭头不同 -> 普通函数。我知道如何转换箭头 -> 正常功能。但是,对于这种情况,我还需要传入 next ,这是我以前没有遇到过的
export default function (field: string) {
return function (next: any) {
(this as any).populate(field);
next();
};
}
在我的猫鼬模式中
projectSchema
.pre("findOne", Populate("nestedComments"))
.pre("find", Populate("nestedComments"));