小编shu*_*hun的帖子

如何在 super() 中使用 configService?

我有一个关于设置环境变量的问题。

在官方文档中,它说在这种情况下使用ConfigModule,但我的情况是一个例外情况。

因为我想在构造函数的 super() 中使用它。

我的代码如下。

这种情况有什么解决办法吗?

如果您需要更多信息,请告诉我。

谢谢大家的支持!!

// jwt.strategy.ts

import { UnauthorizedException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { InjectRepository } from '@nestjs/typeorm';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { JwtPayload } from './jwt-payload.interface';
import { UserRepository } from './user.repository';
export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor(
        @InjectRepository(UserRepository)
        private userRepository: UserRepository,
        private configService: ConfigService,
    ) {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            secretOrKey: configService.get('JWT_TOKEN'),
        });
    }

    async validate(payload: JwtPayload) …
Run Code Online (Sandbox Code Playgroud)

nestjs nestjs-passport nestjs-config nestjs-jwt

10
推荐指数
2
解决办法
4763
查看次数