Mosca给出此错误:
TypeError: Client is not a constructor
at new KafkaAscoltatore (/home/x/Desktop/broker/node_modules/mosca/node_modules/ascoltatori/lib/kafka_ascoltatore.js:59:26)
at Object.build (/home/x/Desktop/broker/node_modules/mosca/node_modules/ascoltatori/lib/ascoltatori.js:77:12)
at /home/x/Desktop/broker/node_modules/mosca/lib/server.js:181:40
at makeCall (/home/x/Desktop/broker/node_modules/fastseries/series.js:117:7)
at ResultsHolder.release (/home/x/Desktop/broker/node_modules/fastseries/series.js:96:9)
at series (/home/x/Desktop/broker/node_modules/fastseries/series.js:39:14)
at Object.series (/home/x/Desktop/broker/node_modules/steed/steed.js:90:7)
at new Server (/home/x/Desktop/broker/node_modules/mosca/lib/server.js:171:9)
at Object.<anonymous> (/home/x/Desktop/broker/broker.js:118:14)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:266:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)
我打开了/home/x/Desktop/broker/node_modules/mosca/node_modules/ascoltatori/lib/kafka_ascoltatore.js
在查看以下行后发现问题与kafka-node版本有关:
this._opts.kafka = this._opts.kafka || require("kafka-node");
删除this._opts.kafka
后,它开始正常工作。然后我打印了两个文件,发现新版本中没有Client构造函数。这是我的打印结果:
this._opts.kafka (kafka-node v4.0.1 latest)
返回不具有客户端的HighLevelProducer,另一方面,require(“ kafka-node”)(v0.5.9)返回HighLevelConsumer。
var backend = {
type: "kafka",
kafka: require('kafka-node'),
json: false,
connectionString: "IP:2181",
defaultEncoding: "utf8",
};
var moscaSettings = {
interfaces: [
{ type: "mqtt", port: PORT }
],
id: "mosca",
stats: false,
publishNewClient: false,
publishClientDisconnect: false,
publishSubscriptions: false,
backend: backend,
};
var server = new mosca.Server(moscaSettings); // Error pops here
如上所述,我可以让Kafka使用旧版本,但是我认为这只是一个快速修复,因此我希望有人可以为我提供适当的解决方案。
答案 0 :(得分:0)
问题是由于我使用旧版本的kafka-node中的示例而导致的。当我使用来自kafka-node GitHub的新示例替换代码时,问题已解决。