In this post I will go through 2 different methods on how to declare a variable to use the same NodeRed flow.
For method 1 I will use the Change node to set/declare the flow variable
Level 1
Drag out and drop a node and configure as below
Pass a string value “this is a string” to msg.payload
level2
Drag out a change node and set as below. The configuration shows that it will pass the data from msg.payload to flow.firstString
After this step, you can use the “firstString” variable inside the flow
Step 3
Connect the nodes together.
You have now completed the node where when the trigger it will inject “This is A String” to msg.payload. The change node will read the msg.payload value
and pass the value into the flow variable “firstString”
Step 4
Retrieve the “firstString” value of the stream variable
Drag out the injection node and set as below.
The Inject Node will pass the value of the stream variable “firstString” into msg.payload
Step 5
Drag out a Debug node, and configure as below
Step 6
Connect the nodes, run both the injection node and view the results
results
For method 1 I will use the Function node to define / declare the flow variable
Level 1
Drag out and drop a node and configure as below
Pass a string value “this is string 3” to msg.payload
level2
Drag out a function node and set as below. The configuration shows that it will pass the data from msg.payload to flow.secondString
After this step, you can use the “secondString” variable inside the flow.
Copy and paste the code below and paste it into the text area inside the “In Message” tab.
Code as below
flow.set(‘secondString’, msg.payload)
return message;
Step 3
Connect the nodes together.
You have now completed the node where when the trigger fires it will inject “This is A String 3” to msg.payload. The change node will read the msg.payload value
and pass the value into the stream variable “secondString”
Step 4
Retrieve the “secondString” value of the stream variable
Drag out the injection node and set as below.
The Inject Node will pass the value of the stream variable “secondString” into msg.payload
Step 5
Drag out a Debug node, and configure as below
Step 6
Connect the nodes, run both the injection node and view the results
results
to check
How to extract JSon values from a NodeRed JSon object