将箭头函数转换为猫鼬中间件的普通函数

时间:2021-07-14 05:03:17

标签: javascript typescript

我喜欢将其转换为函数而不是箭头函数,以用作我的猫鼬模式中的中间件。任何人都可以建议如何做到这一点?谢谢

    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"));

0 个答案:

没有答案
相关问题