Search code examples
node.jsnode-red

How to create a function to access JSON index in node.js?


I am working with node-red and I would like to create my custom function to access some index from the incoming message. The incoming message looks like this

{ "node": "02010101", "base64": "Cro=" }

It comes out from the json function block in node-red (A function that parses the msg.payload to convert a JSON string to/from a javascript object. Places the result back into the payload), I can use the debug block to obtain the index base64, however if I try to do the same with my own function to proces that later, I cannot and I get the error

TypeError: Cannot assign to read only property '_msgid' of Cro=

My function is really silly for now it is just

return msg["base64"];

I understand that it complains that there is no property in the incoming message, so I would like to access to hat index, how can I do it?

EDIT: if I set the debug block to show the whole message object not just the msg.base64 itself, I get this

{ "node": "02010101", "base64": "Cro=", "_msgid": "6babd6e.f945428" }

Image


Solution

  • A function node should return a whole msg object not just a string.

    If you want to just send on the string value you should do something like this:

    msg.payload = msg.payload["base64"];
    return msg