【发布时间】:2019-01-22 09:50:27
【问题描述】:
我有一个用 Javascript ES6 编写的类。当我尝试执行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)
我想要做的是,我创建了一个类,然后创建了该类的一个实例。然后我正在尝试导出该变量。
类结构定义如下:
class FBClient extends FabricClient{
constructor(props){
super(props);
}
<<< FUNCTIONS >>>
}
我如何尝试导出变量 ->
var client = new FBClient();
client.loadFromConfig(config);
export default client = client;
你可以在这里找到完整的代码>https://hastebin.com/kecacenita.js Babel 生成的代码 > https://hastebin.com/fabewecumo.js
【问题讨论】:
-
我不确定您是否可以导出类的实例。我认为您必须导出类,将其导入另一个类,然后在那里创建实例
-
@bwalshy - 您可以导出任何值,包括类的实例。
-
@T.J.Crowder 此代码无法在线运行,因为它需要区块链模块。
-
@AkshaySood - 错误显然与区块链无关,而与基本类定义和实例化有关。请阅读该 MCVE 链接。您不需要发布所有代码。您需要完成一个生成最小复制示例的过程。您甚至没有向我们展示发生错误的类。
-
你在使用 Babel 转译你的代码吗? (possibly related issue, but just a guess)
标签: javascript node.js ecmascript-6