When I tried to run the code below in Visual Studio, I got the following error:
TypeError: eventType.toBuffer is not a function at 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:
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
const avro = require('avsc');
module.exports = avro.Type.forSchema({
type: 'record',
fields: [
{
name: 'category',
type: { type: 'enum', symbols: ['DOG', 'CAT'] }
},
{
name: 'noise',
type: 'string',
}
]
});