Node red variable definition in version 3.0, what's changed?

Hi,

I'm running node red v. 3.0.2.
I have problems to define and initialize variables in the functions "on start" pane and then using them in the "on message" pane.
The editor complains that it "Cannot find name 'xxx'" even when 'xxx' is defined in "on start".

In earlier versions of node red, both in vesions 1 and 2 this worked fine.
So what is changed in version 3, and how to do it?

I want to define and initialize a variable in "on start" and use it in "on message".

Below is a small test function node that illustrates the problem. When running this code, I get error message:"ReferenceError: test_OnStart is not defined (line 1, col 15)"
[
{
"id": "368473e65280b6f8",
"type": "function",
"z": "b451e0f43b8be752",
"name": "function 3",
"func": "msg.payload = test_OnStart;\nreturn msg;",
"outputs": 1,
"noerr": 1,
"initialize": "// Code added here will be run once\n// whenever the node is started.\nvar test_OnStart = "defined"",
"finalize": "",
"libs": ,
"x": 800,
"y": 240,
"wires": [
[
"e2fc02ea587ca4a2"
]
]
}
]

When sharing flows on the forum please take care to format them properly. Use the </> button in the toolbar to add a code block.

Your function is:

msg.payload = test_OnStart;
return msg;

But you haven't defined test_OnStart anywhere - hence the error.

Node-RED applies JavaScript strict mode to the code which ensures errors like this don't get ignored.

I'm almost certain this is always been the case since on start was introduced.

If you want a variable that was created in on start, you need to store it in context then retrieve it in on message.

That is exactly my point:

The test_OnStart variable is defined (and initialized) under the "On Start" pane of the function like this:
var test_OnStart = "defined"

So why does the code in "On Message" not have access to the code in "On Start" ?

Best regards,

As mentioned before, it has always been this way. On start and on message are separate VMs under the hood. You need to use context if you want to transfer the variables to on message.

As Steve says, it has always been the case that they are run in different scopes.

If you want to store/initialise any values, use Context to store them.

Fair enough that you must use context in version 3 of node-red.

But i don't agree this has always been like this.
As an example, I made a quick test example in node red v1.1.3:

I have a inject node, then a function node setting two variables in the "Setup" pane.
This sets the two variables:
test_nr113 = "init_OK"
topic = "node-redv1.1.3"

Then I use these variables in the "Function" pane:
msg.payload = test_nr113;
msg.topic = topic;
return msg;

This works fine without any errors. Debug node prints out the correct payload and topic values.

If I do the same thing in node red v3, I will get errors on undefined variables.

See also attached snapshots.
flows (2).json (1.4 KB)
nodes
setup
function
debug

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