nestjs中,JwtService报错,没有SECRET
                    目录
                    
                
                
            nestjs中,JwtService报错: “JsonWebTokenError: secret or public key must be provided”
原因
最近老是出现这个错误: JsonWebTokenError: secret or public key must be provided,
- 
我已经在环境变量中增加 imports: [JwtService],
- 
也在底层库 shared.module.ts里面增加了:1 2 3 4 5 6 7 8 9 10imports: [ JwtModule.registerAsync({ imports: [ConfigModule], useFactory: (configService: ConfigService) => ({ secret: configService.get<string>('JWT_SECRET') as string, signOptions: { expiresIn: '1d' }, }), inject: [ConfigService], }), ]
结果AuthGurad中的JwtService还是找不到secret, 按照之前的经验,应该是AuthModule中没有正确导入
解决办法
将 auth.module.ts中的imports: [JwtService]换成imports: [SharedModule]
因为调用JwtModule.registerAsync是在SharedModule中,而如果在auth.module.ts中再次直接imports: [JwtModule],可能将原来的覆盖了.