Global variable undefined in subflow?

Look in settings.js for the bit about context.

If it's related to the contextStorage property, then it's in a comment block so it should not play any role here.

Do you mean the contextStorage section is entirely commented out? I find that surprising but maybe it is ok. Are you looking at the file in your .node-red folder.

I guess that isn't the problem anyway, I couldn't think of any other reason it might be undefined.

Is the global var regularly written to? If so is it possible that it is has been written to with undefined but then gets returned to a sensible value before you refresh the view in the editor? It might be worth tracking down all places it is written to and add traps to check that it is not writing undefined.

[Edit] just a check, do you realise the view in the editor only updates when you click the refresh button?

Yepp, the complete section is commented out. I'm 100% sure that I checked the right file in the right folder.

That global variable is only set once when nodered starts up and never again. I double checked it now by searching for:
global.set("farmName
global.set('farmName

and the editor gives only that one function which is connected to the inject that triggers only on startup.

Refreshing the global context in the editor does not change anything with regards to that variable.

I also tried reading that variable simply by a manual inject node which I connected to a debug node and the value is correctly read. However, that I could only check on a normal tab not in the subflow's tab -that's why I suspect that it has to do something with being read in a subflow.

Are you certain the variable doesn't have a space at the beginning or end?

You can use global.keys() to get a list of keys in global - send that out to a debug node and inspect them

e.g. inject --> function node --> debug node

Function code...

msg.payload = global.keys();
return msg;

I also suspected that but already ruled out. Checking all the keys gives:

image

Does that global always show undefined in the subflow, or is it intermittent?
Are there any other global vars used in the subflow?

What I don't understand is there is more to that warn statement than just getting the global.

Can you try node.warn(["farmName", global.get("farmName")])

1 Like

Perhaps you are trying to access the parent context of the subflow ? If this is the case you should test prepending $parent to the name of the scope variable.

There are a couple of others that are undefined as well which are also set only on startup. But I noticed that there's one (which is also set on startup only) and it's not empty. Funnily enough that's the variable that holds the email addresses that's why the email gets sent but with undefined values in it :slight_smile:

I don't know why it appears as if there were more node.warns() either. I suspected that's because of the first new line in the string logged but no clue why all other new line chars later are not treated the same way.

Not really. In case of global variables there's no parent context like for flow.get("$parent.whatever").

Did you try this ↓

What did you get?

Forgot it but now gave it a try via an inject node and I got:

image

I also added it to the subflow but it takes time till it gets triggered.

Can you post the full code from this snipped (as text)?

PS does this ↑ run in a subflow?

Sure. That's a function node I placed before the email node to filter and log the garbage and also to avoid that the email recipients are spammed with nonsense. This (and the related flow of course) run in a subflow.

if(msg.topic.startsWith("undefined")===true){
    node.warn("Phantom farm:"+global.get('farmName')+"\nPhantom payload:"+JSON.stringify(msg.payload)+"\nPhantom event:"+JSON.stringify(msg.event));
    node.warn(["farmName", global.get("farmName")]);
    return null;
}
else return msg;

I had an idea so I created a new subflow, simply added a function node which reads the global variable and connected it to a debug node. When I trigger this subflow from outside with an inject node, the variable is read correctly so it does not seem to be a general issue of reading a global variable in a subflow. I'll investigate it further but nodered seems to be fine. However, if anyone has any idea on how to catch this red handed, it's welcome :slight_smile:

Is it possible it is just happening on a restart (before the variable is initialised) and you are getting occasional restarts that you are not aware of?

I already wanted to report that I managed to solve it yesterday and the issue was indeed what you guessed :slight_smile: The app crashes sometimes due to insufficient memory (1GB) on raspberry pi 3B+ but as systemd is configured to restart it, it happens again and again. So we're currently optimising the use of memory mainly doing certain things at file system level instead of doing it in memory.

Thanks again to all for your efforts and time!

1 Like

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