6

我一直在关注 jwt 示例,例如https://docs.nestjs.com/techniques/authentication。我复制并粘贴了示例。在 npm 安装必要的位和 bops 后,我得到了这个错误,这在我刚刚复制的示例中没有发生。我不知道这意味着什么!有人有什么想法吗?

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)
4

2 回答 2

5

项目缺少@types/passport-jwt类型,因此应额外安装它们:

npm i -D @types/passport-jwt

这导致

src\auth\jwt.strategy.ts (10,6):调用目标不包含任何签名。(2346)

错误,因为@nestjs/passport没有正确输入;PassportStrategy返回类型是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 {
...
于 2018-06-02T23:51:37.720 回答
0

在我的情况下,问题出在 tsconfig.ts 文件中,我使用的是 nest.js,在 cli 命令之后typeorm init它会覆盖 tsconfig,所以npm run start:dev抛出异常:TypeError: Class constructor MixinStrategy cannot be invoked without 'new'

注意:也用旧版本覆盖 package.jsontypescipt, @ts/node, ts-node

于 2020-12-15T07:57:24.380 回答