我正在使用 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/