Nestjs 如何在super()中配置用户服务?
我有一个关于设置环境变量的问题 在官方文件中,它说在这种情况下使用ConfigModule,但我的情况是一个例外情况 因为我想在构造函数的super()中使用它 我的代码如下 在这种情况下有什么解决办法吗 如果你需要更多的信息,请告诉我 谢谢大家的支持Nestjs 如何在super()中配置用户服务?,nestjs,nestjs-config,nestjs-passport,nestjs-jwt,Nestjs,Nestjs Config,Nestjs Passport,Nestjs Jwt,我有一个关于设置环境变量的问题 在官方文件中,它说在这种情况下使用ConfigModule,但我的情况是一个例外情况 因为我想在构造函数的super()中使用它 我的代码如下 在这种情况下有什么解决办法吗 如果你需要更多的信息,请告诉我 谢谢大家的支持 //jwt.strategy.ts 从'@nestjs/common'导入{UnauthorizedException}; 从'@nestjs/config'导入{ConfigService}; 从'@nestjs/passport'导入{Pas
//jwt.strategy.ts
从'@nestjs/common'导入{UnauthorizedException};
从'@nestjs/config'导入{ConfigService};
从'@nestjs/passport'导入{PassportStrategy};
从'@nestjs/typeorm'导入{InjectRepository};
从“passport jwt”导入{Strategy,ExtractJwt};
从“./jwt payload.interface”导入{JwtPayload};
从“./user.repository”导入{UserRepository};
导出类JwtStrategy扩展了PassportStrategy(策略){
建造师(
@注入存储库(UserRepository)
私有userRepository:userRepository,
专用配置服务:配置服务,
) {
超级({
jwtFromRequest:ExtractJwt.FromAuthHeaderAsberToken(),
secretrokey:configService.get('JWT_令牌'),
});
}
异步验证(有效负载:JwtPayload){
const{username}=有效载荷;
const user=wait this.userRepository.findOne({username});
如果(!用户){
抛出新的UnauthorizedException();
}
返回用户;
}
}
对于您的策略
您缺少@Injectable()
,它告诉Nest它需要注入构造函数中定义的依赖项。对于策略
您缺少@Injectable())
,它告诉Nest需要注入构造函数中定义的依赖项。您需要将configModule导入模块类,configService才能工作。另外,在类名的正上方添加@Injectable(),以指示它是提供程序
这就是导入模块的方式
//auth.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { JwtStrategy } from './jwt.strategy';
@Module({
imports: [ConfigModule],
provider:[JwtStrategy]
})
export class AuthModule {}
NestJs解析它们之间的依赖关系
请参阅:您需要将configModule导入模块类,configService才能工作。另外,在类名的正上方添加@Injectable(),以指示它是提供程序 这就是导入模块的方式
//auth.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { JwtStrategy } from './jwt.strategy';
@Module({
imports: [ConfigModule],
provider:[JwtStrategy]
})
export class AuthModule {}
NestJs解析它们之间的依赖关系
请参阅:检查此答案可能对您有效:检查此答案可能对您有效: