message.data.toString() returned the appropriate JSON string.
How to convert AQMP message buffer into JSON object when using node.js amqp module?
-
02-09-2022 - |
سؤال
I am using node.js amqp module for reading messages from a queue. The following is the callback that is invoked when there is a message available on the queue:
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
}
Thanks.
المحلول 2
نصائح أخرى
If you receive a Buffer that contains JSON, then you'll need to convert it to a string to output something meaningful to the console:
console.log(message.toString())
If you want to convert that string to a full JavaScript object, then just parse the JSON:
var res = JSON.parse(message.toString())
Edit: node-amqp seems to be able to send directly JavaScript objects (see here), you shouldn't be receiving buffers but instead JavaScript objects... Check how you send your messages.
لا تنتمي إلى StackOverflow