Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript TypeError:如果没有';新';_Typescript_Jwt_Passport.js_Nestjs - Fatal编程技术网

Typescript TypeError:如果没有';新';

Typescript TypeError:如果没有';新';,typescript,jwt,passport.js,nestjs,Typescript,Jwt,Passport.js,Nestjs,我一直在关注jwt的例子,就像在这里找到的一样。我复制并粘贴了示例。在npm安装了必要的bits和BOP之后,我得到了这个错误,我刚才复制的样本中没有出现这个错误。我不知道这是什么意思!有人有什么想法吗 TypeError: Class constructor MixinStrategy cannot be invoked without 'new' 8 | export class JwtStrategy extends PassportStrategy(Strategy) {

我一直在关注jwt的例子,就像在这里找到的一样。我复制并粘贴了示例。在npm安装了必要的bits和BOP之后,我得到了这个错误,我刚才复制的样本中没有出现这个错误。我不知道这是什么意思!有人有什么想法吗

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)
缺少
@types/passport jwt
打字,因此应额外安装:

npm i -D @types/passport-jwt
这导致

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

错误,因为未正确键入
@nestjs/passport

为了解决这个问题,

@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构造函数mixinsttrategy在没有“new”的情况下无法调用


注意:is也会使用较旧版本的
typescipt、@ts/node、ts node

覆盖package.json。您能否提供一种方法来复制问题-回购等?可能特定于软件包版本或其他任何版本。当然,克隆此文件夹npm i&&npm start它将显示在windows 10和节点8.11.2上。我无法复制您遇到的错误。混合策略没有问题。您当前的代码可能与链接的代码不同。但我打字有问题。我建议在和关于不正确的PassportStrategy类型中打开一个关于此示例的问题(19 auth misses@types/passport jwt dependency)。无论如何,我无法使这个例子可行;服务器运行,但响应404。不确定此时是否可以执行任何操作。我曾认真考虑将NestJS作为我的下一个框架,但它似乎还不够成熟。文档有点缺乏,没有开箱即用的示例也没有真正的帮助。感谢您将此问题带到这里,这让人大开眼界。请检查您的tsconfig.json以确保“target”是“es6”。此问题已在
1.0.11
补丁版本中得到解决。在tsconfig.json中将“target”设置为“ES5”时,原始代码和此错误都会引发“TypeError:Class构造函数Mixinsttrategy无法在没有“new”的情况下调用”。ES5有办法吗@estus@Smartkid我希望对于ES5目标,因为TS使用已建立的继承方法,
\u this=Parent.call(this)
。需要传输整个类层次结构以使其正常工作,否则将导致错误,因为在没有
new
的情况下无法调用本机类。你为什么要在Node中使用ES5 target?@estus谢谢你的解释。我使用Thread workspace在nestjs服务器项目和基于react的项目(针对ES5以避免babel)之间共享come代码。旧版本WebStorm的Jest支持似乎使用工作区根目录下的tsconfig运行单元测试,而忽略子项目下的tsconfig。看起来WebStorm在2018.1.4中解决了这个问题。@Smartkid感谢您提供有关WebStorm的信息,很高兴知道,