我跟随着jwt示例,例如在https://docs.nestjs.com/techniques/authentication中找到的。我复制并粘贴了示例。在npm安装必要的位和行之后,我得到了这个错误,该错误在我刚刚复制的样本中没有发生。我不知道这意味着什么!有人有想法吗?
TypeError: Class constructor MixinStrategy cannot be invoked without 'new'
8 | export class JwtStrategy extends PassportStrategy(Strategy) {
9 | constructor(private readonly authService: AuthService) {
> 10 | super({
11 | jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
12 | secretOrKey: 'secretKey',
13 | });
at new JwtStrategy (data/auth/strategies/jwt.strategy.ts:10:5)
at resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:64:84)
at Injector.resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:86:30)
最佳答案
project缺少@types/passport-jwt
类型,因此应另外安装它们:
npm i -D @types/passport-jwt
这导致
错误,因为没有正确键入
@nestjs/passport
; PassportStrategy
return type is any
。为了解决这个问题,
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
...
应该更改为:
import { ExtractJwt, Strategy } from 'passport-jwt';
import { AbstractStrategy, PassportStrategy } from '@nestjs/passport';
...
const PassportJwtStrategy: new(...args) => AbstractStrategy & Strategy = PassportStrategy(Strategy);
@Injectable()
export class JwtStrategy extends PassportJwtStrategy {
...
关于typescript - TypeError : Class constructor MixinStrategy cannot be invoked without 'new' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50654877/