Search code examples
javascriptnode-red

Node-red I can't extract the message body


I started using Node-Red to develop an MQTT dashboard, I just starting with a simple example to subscribe to a topic and debug the message. However, I couldn't get the json value of my message and convert it from base64 to a string.

my message looks as fellow:

"{"metrics":{},"body":"eyJjb250ZW50LXNwZWMiOiJ1cm46c3BlYzpcL1wvZWNsaXBzZS5vcmdcL3VuaWRlXC9tZWFzdXJlbWVudC1tZXNzYWdlI3YyIiwiZGV2aWNlIjp7ImRldmljZUlEIjoiMTMyNDUifSwibWVhc3VyZW1lbnRzIjpbeyJzZXJpZXMiOnsiJF90aW1lIjpbMF0sIlZBIjpbMjM5MzMwLjBdfSwidHMiOiIyMDE4LTA0LTI1VDA5OjM4OjU0LjIyOCswMDAwIn1dfQ=="}"

I want to extract the body message and convert it.

I already tried this function after using a JSONconverter:

return msg.payload.body;

but I got his error: "Function tried to send a message of type string"

nodes

message

Best regards


Solution

  • A Function node must return a message object - not an plain value. That is why you are getting the error messaging telling you the Function is sending a String.

    If you want the payload of the message it sends to be that string value, you would do:

    msg.payload = msg.payload.body;
    return msg;
    

    For that type of thing, you would be better served using the Change node to move msg.payload.body to msg.payload.