【问题标题】:NodeJS: How to send entity from mysql to rabbitmqNodeJS:如何将实体从 mysql 发送到 rabbitmq
【发布时间】:2021-09-08 12:33:53
【问题描述】:

我正在尝试将从 MySQL 获得的“实体”发送到 RabbitMQ。

我能够连接到数据库并返回数据。示例:

    dbConnection.query("SELECT * FROM customer WHERE Id = ?", customerId, (err, rows, fields) => {
    ...
    res.status(200).json(rows)
    ...
    }

之后我可以在 Postman 中看到“JSON 结果”,因此,我想将此“JSON 结果”作为字符串发送到 RabbitMQ。

我可以毫无问题地向 RabbitMq 发送一个假数据对象:

    const fakeData = {
        name: "Elon Musk",
        company: "SpaceX",
    };

    channel.sendToQueue("message-queue", Buffer.from(JSON.stringify(fakeData)));

那么,我必须如何转换从 MySQL 返回的“行”对象以将其发送到队列?

提前谢谢你!

【问题讨论】:

    标签: mysql node.js rabbitmq


    【解决方案1】:

    我的问题的解决方法如下:

    rows.forEach(function (row) {
        channel.sendToQueue("message-queue", Buffer.from(JSON.stringify(row)));
    });
    

    【讨论】:

      猜你喜欢
      • 2021-09-26
      • 1970-01-01
      • 2019-07-07
      • 2015-08-30
      • 2016-04-19
      • 1970-01-01
      • 2019-07-22
      • 2021-12-27
      • 1970-01-01
      相关资源
      最近更新 更多