TypeError: Class constructor MixinStrategy cannot be invoked without 'new'
TypeError: Class constructor MixinStrategy cannot be invoked without 'new'
我正在关注此处 https://docs.nestjs.com/techniques/authentication 中的 jwt 示例。我复制并粘贴了示例。在 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)
project 缺少 @types/passport-jwt
类型,因此应额外安装它们:
npm i -D @types/passport-jwt
这导致
src\auth\jwt.strategy.ts (10,6): Call target does not contain any signatures. (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 {
...
在我的案例中,问题出在 tsconfig.ts 文件中,我正在使用 nest.js 并且在 cli 命令 typeorm init
之后它覆盖了 tsconfig,因此 npm run start:dev
抛出异常:TypeError: Class 没有 'new'
就无法调用构造函数 MixinStrategy
注意:package.json 也被旧版本 typescipt, @ts/node, ts-node
覆盖
我正在关注此处 https://docs.nestjs.com/techniques/authentication 中的 jwt 示例。我复制并粘贴了示例。在 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)
project 缺少 @types/passport-jwt
类型,因此应额外安装它们:
npm i -D @types/passport-jwt
这导致
src\auth\jwt.strategy.ts (10,6): Call target does not contain any signatures. (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 {
...
在我的案例中,问题出在 tsconfig.ts 文件中,我正在使用 nest.js 并且在 cli 命令 typeorm init
之后它覆盖了 tsconfig,因此 npm run start:dev
抛出异常:TypeError: Class 没有 'new'
注意:package.json 也被旧版本 typescipt, @ts/node, ts-node