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