node.js - 使用 node.js amqp 模块时如何将 AQMP 消息缓冲区转换为 JSON 对象?

标签 node.js type-conversion node-amqp

我正在使用 node.js amqp 模块从队列中读取消息。以下是队列中有可用消息时调用的回调:

function onMessage(message, headers, deliveryInfo)
{
    console.log(message); //This prints buffer
    //how to convert message (which I expect to be JSON) into a JSON object.
    //Also how to get the JSON string from the 'message' which seems to be a buffer
}

谢谢。

最佳答案

如果您收到一个包含 JSON 的 Buffer,那么您需要将其转换为字符串以向控制台输出有意义的内容:

console.log(message.toString())

如果您想将该字符串转换为完整的 JavaScript 对象,则只需解析 JSON:

var res = JSON.parse(message.toString())

编辑: node-amqp 似乎能够直接发送 JavaScript 对象(见 here ),你不应该接收缓冲区,而是 JavaScript 对象...检查你如何发送你的消息。

关于node.js - 使用 node.js amqp 模块时如何将 AQMP 消息缓冲区转换为 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20627559/

相关文章:

javascript - Mocha 中的构造函数无法识别

javascript - 从 html 页面执行 Nodejs 脚本?

.net - 如何在运行时转换类型?

javascript - 为什么带有amqplib Consumer函数的nodejs是闭包?

node.js - node-amqp 队列被破坏 : notifications to subscribers

node.js - 当node-amqp出现问题时,为什么会出现 "close"事件?

node.js - SailsJs res.render 与 res.view

javascript - mailgun 在 node.js 中抛出错误

Excel VBA 将所有内容转换为文本

r - 为什么 `as`方法删除矢量名称,并且有解决方法?