TypeError:客户端不是构造函数-最新版本的kafka-node错误

时间:2019-02-05 11:13:27

标签: node.js apache-kafka mosca

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)

环境

  • 节点版本:v10.9.0
  • Kafka节点版本:最新
  • Mosca版本:最新

我尝试过的

我打开了/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使用旧版本,但是我认为这只是一个快速修复,因此我希望有人可以为我提供适当的解决方案。

1 个答案:

答案 0 :(得分:0)

问题是由于我使用旧版本的kafka-node中的示例而导致的。当我使用来自kafka-node GitHub的新示例替换代码时,问题已解决。