Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

499
Views
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:

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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',
    }
  ]
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!