简体   繁体   English

我运行了我的代码并得到了这个错误:TypeError: eventType.toBuffer is not a function

[英]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.

相关问题 当我使用 expo 在手机中运行我的代码时,React Native 出现此错误 - React Native got this error when i ran my code in phone using expo 我的异步 function 出现语法错误 - I got a syntax error in my async function 我收到错误“TypeError:products.map 不是函数” - I got the error "TypeError: products.map is not a function" 我创建了这个 React 应用程序来获取我创建的 API,但收到错误消息“TypeError: video.map is not a function”。 下面是代码: - I created this React application to a fetch API that I created, but got the error message "TypeError: video.map is not a function". Below is the code: 我进行测验,并通过shiftedit运行js代码,并收到以下错误:&#39;answerValue&#39;超出范围:lin 15 - I am making a quiz and I ran js code through shiftedit and got the following error: 'answerValue' used out of scope: lin 15 TypeError: path.join is not a function(在我的 handleEvents.js 文件中出现错误) - TypeError: path.join is not a function (got the error in my handleEvents.js file) 我收到一个错误:未捕获(承诺)类型错误:cartItems.map 不是 function。 上述错误发生在<cartscreen>零件:</cartscreen> - i got an error: Uncaught (in promise) TypeError: cartItems.map is not a function. The above error occurred in the <CartScreen> component: 我在 React 上尝试在我的待办事项列表上呈现过滤器功能时收到“未捕获的类型错误:未定义不是函数” - I got an "Uncaught TypeError: undefined is not a function" while trying to render the filter functionality on my todo list on React 在 Django 模板中编写 javascript 代码我收到错误“未捕获的类型错误:无法设置属性 'innerHTML' of null” - Writing javascript code in Django templates I got an error “ Uncaught TypeError: Cannot set property 'innerHTML' of null” 即使我的代码看起来很好,我仍然会收到“未捕获的TypeError:字符串不是函数”错误。任何人都可以看看并解释一下吗? - I keep getting a “Uncaught TypeError: string is not a function” error, even though my code seems to be fine. Can anyone have a look and explain?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM