我有一个用Javascript ES6编写的类.当我try 执行nodemon命令时,我总是看到这个错误TypeError: Class constructor Client cannot be invoked without 'new'

完整错误如下所述:

/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:45
        return (0, _possibleConstructorReturn3.default)(this, (FBClient.__proto__ || (0, _getPrototypeOf2.default)(FBClient)).call(this, props));
                                                                                                                              ^

TypeError: Class constructor Client cannot be invoked without 'new'
    at new FBClient (/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:45:127)
    at Object.<anonymous> (/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:195:14)
    at Module._compile (module.js:641:30)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Module.require (module.js:585:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/akshaysood/Blockchain/fabricSDK/dist/routes/users.js:11:20)

我想做的是,我创建了一个类,然后创建了该类的一个实例.然后我试图导出这个变量.

课程 struct 定义如下:

class FBClient extends FabricClient{

    constructor(props){
        super(props);
    }

<<< FUNCTIONS >>>

}

How I am trying to export the variable ->

var client = new FBClient();
client.loadFromConfig(config);

export default client = client;

You can find the full code here > https://hastebin.com/kecacenita.js Code generated by Babel > https://hastebin.com/fabewecumo.js

推荐答案

问题是,该类扩展了本机ES6类,并通过Babel传输到ES5.传输类不能扩展本机类,至少在没有额外措施的情况下是这样.

class TranspiledFoo extends NativeBar {
  constructor() {
    super();
  }
}

结果是

function TranspiledFoo() {
  var _this = NativeBar.call(this) || this;
  return _this;
}
// prototypically inherit from NativeBar 

由于ES6类应该只使用new调用,因此NativeBar.call会导致错误.

ES6类在任何最新的 node 版本中都受支持,它们不应该被传输.es2015应该排除在Babel配置之外,最好使用env preset set to node target.

同样的问题applies to TypeScript.编译器应该正确配置为不传输类,以便它们从本机或Babel类继承.

Node.js相关问答推荐

如何在不丢失其他键的情况下解开子文档数组,然后反转该过程?

使用 playwright 获取页面中同一 url 的所有响应

在nodejs中为oauth请求创建和存储csrf令牌的最佳方法

发布请求不使用 Nodejs 更新 MongoDB 中的标头信息

将 null 推入管道后,node.js 可写完成未发出

Cloudflare 522 错误 - javascript 客户端连接到 node 服务器

将代码转换为 ES6 Discord.js 的问题

Typescript typeRoots 未检测到类型定义

使用正则表达式查找文档,但输入是数组

如何使用 node 在 koa.js 中发送响应

Sharp JS 依赖关系 destruct 了 Elastic Beanstalk 上的 Express Server

eslint - vscode 的可选链接错误

Node.js 服务器中没有缓存

如何在 AWS 上的 Amazon Linux AMI 中自动启动 node.js 应用程序?

React Native ios build:找不到 node

如何让should.be.false语法通过 jslint?

如何解决'npm应该在 node repl之外运行,在你的普通shell中'

如何从 npm 注册表中删除 npm 包?

如何在离线时安装 npm 包?

当进程被杀死时,如何优雅地关闭我的 Express 服务器?