我已在我的 UserModule 中將 JwtModule 注冊為:
@Module({
imports: [
// MongooseModule.forFeature([{ name: User.name, schema: userSchema }]),
JwtModule.register({
secret: process.env.JWT_TOKEN,
signOptions: { expiresIn: '60s' },
}),
],
controllers: [UserController],
providers: [UserService, UserRepository],
exports: [UserService, UserRepository],
})
我在我的 UserModule 中注冊了中間件:
export class UserModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(CurrentAuth).forRoutes({
path: '*',
method: RequestMethod.GET,
});
}
}
在我的 CurrentAuth 中間件中,我將 JwtService 初始化為:
export class CurrentAuth implements NestMiddleware {
constructor(private tokenService: JwtService) {}
use(req: Request, res: Response, next: NextFunction) {
const payload = this.tokenService.verify("mytoken");
next();
}
}
但這給了我錯誤:
TypeError: Cannot read properties of undefined (reading 'verify')
請幫助我撰寫代碼,或者如果您知道的話,請提供一些建議。
uj5u.com熱心網友回復:
看起來你在課堂上錯過@Injectable()了CurrentAuth。這是必需的,以便 Typescript 發出建構式引數的元資料。確保您還將中間件添加到providers陣列中
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/524768.html
