How to pass the property name as a variable?

Hello Community,

i m trying to create a subflow where the "Environment variables" of the subflow is a property name.

so inside my subflow i could have a function that can then access the value in the property such as:
msg.payload.{some_variable_entered_from_the_sub_flow_variables_box}

is there a way to do that?

yes.

You can use square bracket notation

example...

var prop = "payload";
var value = msg[prop];

or the node-red helper function RED.util.getObjectProperty...

var prop = "payload";
var value = RED.util.getObjectProperty(msg,prop);

The getObjectProperty helper function will also get nested properties e.g...

var prop = "payload.data.value";
var value = RED.util.getObjectProperty(msg,prop);

As for getting the "Environment variables" - assume you know how to do that already?

1 Like

thank you for the prompt reply Steve,

I assume getting the Environment Variables is done by using: ${enviroment_variable}

i tried that in a Function like this: but did not work :frowning:

var prop = ${enviroment_variable};
var value = msg[prop];

Hi @akd02

The docs for using environment variables in your flows are here: Using environment variables : Node-RED

Thanks guys! truly appreciate it

i got it now... below is my working example:

var prop = env.get("Property");

//var value = msg[prop]; // did not work :frowning:
var value = RED.util.getObjectProperty(msg,prop); //worked ok!!! :slight_smile:

msg.payload = value; // returns the value inside the Property that was set in the Subflow's template

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.