【问题标题】:Nest.js this not working in constructor superNest.js 这在构造函数中不起作用
【发布时间】:2020-12-15 19:31:49
【问题描述】:

我尝试将configService 添加到 JwtStrategy:

  constructor(readonly configService: ConfigService) {
    super({
      //...
      secretOrKey: this.configService.get('SECRET_KEY'),
    });
  }

我正在使用这个instruction。但是 TS 给我报错:

'super' 必须在 a 的构造函数中访问 'this' 之前调用 派生类。

当我删除 this(就像在这个 answer 中一样)时,Nest 返回另一个错误:

Nest 无法解析 JwtStrategy (?) 的依赖关系。请做出来 确保索引 [0] 处的参数 ConfigService 在 AuthModule 上下文。

【问题讨论】:

    标签: node.js this


    【解决方案1】:

    我通过在auth.module.ts 文件中将ConfigService 添加到providers 来修复它:

    @Module({
      controllers: [AuthController],
      providers: [AuthService, JwtStrategy, ConfigService],
      exports: [JwtStrategy],
    })
    

    我必须从 constructor 中删除 private 并从 configService 中删除 this

    【讨论】:

      猜你喜欢
      • 2014-08-24
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多