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"
]
]
}
]
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.
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.