我有一个用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相关问答推荐

nest js控制器方法调用两次

为什么我的表单数据在我的POST请求中作为应用程序/json发送,为什么它返回错误请求错误?

使用xml-crypto时出现NodeJS XPath解析错误

无法从MongoDB文档中保存的对象数组中获取对象的属性

后端如何保护数据不被用户提取其他用户的数据?

通过 Node js 中的 TBA 执行 netsuite REST upsert 操作出现 401 错误

Docker node_modules 文件夹上的 React 应用程序不可用

为什么我不能将 id 发送到后端并通过 findByIdAndRemove() 删除项目?

每个数组值在 mongodb 中查找一个文档

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

Node.js 大文件上传到 MongoDB 阻塞了事件循环和工作池

无法更新MongoDB中的文档:";伯森场';writeConcern.w';是错误的类型';数组'&引用;

如何防止 node.js 中的内存泄漏?

如何在 NodeJS 中获取操作系统用户名?

ISODate 未定义

Mongoose:查找、修改、保存

如何在 MongoDB 上只收听 localhost

npm WARN 不推荐使用 graceful-fs@3.0.8:graceful-fs 版本 3

Javascript在try块内设置const变量

在 Node.js 中获取终端的宽度