TypeError:如果没有' new',则无法调用类构造函数MixinStrategy。

时间:2018-06-02 08:13:49

标签: typescript jwt passport.js nestjs

我跟随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)

2 个答案:

答案 0 :(得分:2)

project缺少@types/passport-jwt类型,因此应另外安装:

npm i -D @types/passport-jwt

这导致

  

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

错误,因为@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 {
...

答案 1 :(得分:0)

在我的情况下,问题出在tsconfig.ts文件中,我使用的是nest.js,在cli命令typeorm init之后,它覆盖了tsconfig,所以npm run start:dev抛出了异常:没有'new'不能调用MixinStrategy

注意:还会用旧版本的typescipt, @ts/node, ts-node覆盖package.json

相关问题