Trouble with flow.get

Hi,

I am trying to use a switch in my UI and update the value of the switch (true or false) in a function node in another part of my flow. Based on the switch output, I am trying to use flow.get in the function node to set a variable. However, when I try to use flow.get, it just sets the variable in the function node to "null".

Below is my flow with the switch, and the code in the connected function node:

image

var btnOpOverride = msg.payload;
msg.payload = btnOpOverride;
return msg;

The code in the other function node in the flow looks like the following, but when I run this I get the default value for the first go around for the OperatorOverride variable, and then a "null" value the 2nd time around:

OperatorOverride = flow.get("btnOpOverride");

What am I doing wrong? I also tried some other things but nothing seems to work. I also tried doing flow.get and flow.set from the function node close to the switch, but that did not work either.

Thanks in advance for your help!

jstou

Somewhere you need to do
flow.set("btnOpOverride", somevalue)
otherwise flow.get has nothing to get.

Is this on another tab? If so, what you see is proper - go read https://nodered.org/docs/user-guide/context

Thanks for the tip! It was not clear to me in the documentation that flow.get and flow.set could be in different function nodes, I thought they had to be in the same function node. Now it makes more sense!

No they are in the same flow. I've figured it out, I needed to use flow.set in the switch function node. I've read all these and they are helpful, but sometimes there is no answer online for a specific problem you are encountering...unless you ask for help on the Node Red Forum!!

If you want to retain data for re-use only within a single node (at a later time) then use context.get/set, if you want it to be accessible across multiple nodes in one flow (ie editor tab) then use flow.get/set, if you want to be able to access the data anywhere then use global.get/set

2 Likes