[英]I ran my code and got this error: TypeError: eventType.toBuffer is not a function
When I tried to run the code below in Visual Studio, I got the following error:当我尝试在 Visual Studio 中运行以下代码时,出现以下错误:
TypeError: eventType.toBuffer is not a function at queueRandomMessage TypeError:eventType.toBuffer 不是 queueRandomMessage 中的函数
Code:代码:
const Kafka = require('node-rdkafka');
const eventType = require('../eventType.js');
const stream = Kafka.Producer.createWriteStream({
'metadata.broker.list': 'localhost:9092'
}, {}, {
topic: 'test'
});
stream.on('error', (err) => {
console.error('Error in our kafka stream');
console.error(err);
});
function queueRandomMessage() {
const category = getRandomAnimal();
const noise = getRandomNoise(category);
const event = { category, noise };
const success = stream.write(eventType.toBuffer(event));
if (success) {
console.log(`message queued (${JSON.stringify(event)})`);
} else {
console.log('Too many messages in the queue already..');
}
}
function getRandomAnimal() {
const categories = ['CAT', 'DOG'];
return categories[Math.floor(Math.random() * categories.length)];
}
function getRandomNoise(animal) {
if (animal === 'CAT') {
const noises = ['meow', 'purr'];
return noises[Math.floor(Math.random() * noises.length)];
} else if (animal === 'DOG') {
const noises = ['bark', 'woof'];
return noises[Math.floor(Math.random() * noises.length)];
} else {
return 'silence..';
}
}
setInterval(() => {
queueRandomMessage();
}, 3000);
This is the code for eventType.js that I used:这是我使用的 eventType.js 的代码:
const avro = require('avsc');
module.export = avro.Type.forSchema({
type: 'record',
fields: [
{
name: 'category',
type: { type: 'enum', symbols: ['DOG', 'CAT'] }
},
{
name: 'noise',
type: 'string',
}
]
});
What should I do to fix this error?我应该怎么做才能修复这个错误?
It should be module.exports
它应该是module.exports
const avro = require('avsc');
module.exports = avro.Type.forSchema({
type: 'record',
fields: [
{
name: 'category',
type: { type: 'enum', symbols: ['DOG', 'CAT'] }
},
{
name: 'noise',
type: 'string',
}
]
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.