Global variable undefined in subflow?

Hi All,

I've just bumped into a problem and I just can't believe my eyes. One of our systems started to send emails with undefined values where no such values should appear. So I started to investigate the email sending subflow and added a filter condition with a node.warn() to log it and also checked the global context variable in the editor.

This is how it looks like in the editor:

And this is what I get in the syslog from node.warn():

What you should notice is that the global variable 'farmName' has a value in the editor when checking the global context but it is undefined when it got logged by the subflow.

Does anyone have any idea? This system has recently been upgraded to 1.2.9. Previously, it was running fine on 1.1.3.

Best regards,
r0ller

Are you using multiple context stores? If so are you looking at the correct store in the editor?

Well, I don't think so :slight_smile: How can I check if multiple context stores are used?

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: