# Variable between "On Start" and "On Message"

**URL:** https://discourse.nodered.org/t/variable-between-on-start-and-on-message/84216
**Category:** General
**Tags:** function-node
**Created:** [2 January 2024 16:58 UTC](https://discourse.nodered.org/t/variable-between-on-start-and-on-message/84216 "2024-01-02T16:58:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)
#### Post date: [2 January 2024 16:58 UTC](https://discourse.nodered.org/t/variable-between-on-start-and-on-message/84216/1 "2024-01-02T16:58:56Z")

</div>

Hello

I have noticed a behavior difference between the Function Node between v2.x and v3.x

With v2.x, I used to have some const arrays setup in "On Start" and using them in "On Message"  
Something like:  
On Start:

```auto
misensors = { 
  "ATC354cc1": "salon_sam",
  "ATCa8ac99": "chbre_jmv",
  "ATCad1f66": "salon_tour",
  "ATC2bfbdf": "chbre_rose"
};

```

Then in the On Message, I was using that variable

```auto
...
if ( device in misensors) device = misensors[device];
...

```

When I upgraded to v3.x, everything was still working fine until I edited the function, then errors were pointed as I was using undeclared variabled.  
Adding `var` or `const` keywords didn't helped so I had to move all those arrays to the On Message tab (adding the `const` keyword).

So my question 1: What did I missed ? What would have been the best way to code that ?

And my question 2: Now I need some non-constant variables that have to be shared across messages  
Should I use `context.set()`/`context.get()` ?  
Isn't there a way to use simple variables when I don't need the value to be really stored ?

thanks

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [2 January 2024 17:17 UTC](https://discourse.nodered.org/t/variable-between-on-start-and-on-message/84216/2 "2024-01-02T17:17:22Z")

</div>

> [@Barbudor](#):
>
> When I upgraded to v3.x, everything was still working fine until I edited the function, then errors were pointed as I was using undeclared variabled.

your variables were always undefined but in V3.x, a better editor is used by default to help users minimise coding bugs. (you can turn this off but it is far better to improve your coding)

> [@Barbudor](#):
>
> Should I use `context.set()`/`context.get()` ?

Yes, always

> [@Barbudor](#):
>
> Isn't there a way to use simple variables when I don't need the value to be really stored

Dont store anything if you dont need it! I am probably missing your point, so post an exported flow and I will evaluate / help you improve.

---

<div class="post-metadata">

### Author: ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)
#### Post date: [2 January 2024 20:26 UTC](https://discourse.nodered.org/t/variable-between-on-start-and-on-message/84216/3 "2024-01-02T20:26:36Z")

</div>

Hi @Steve-Mcl

Thanks  
By "stored" I meant "stored to filesystem" as I have configured the context to be stored by default to filesystem. Actually I am storing in (flow.)context values that are critical and that should be preserved in case node-red restarts.

But I have 2 other use-cases:

- Case 1 : constants such as mapping tables. Those need to be set once for all at deploy/start of NR and doesn't need to be modified by code. I know about the improvement of the editor, great addition. But beside that, it was working in v2 to declare those kind of variables in "On Start" even not specifying `var` or `const`.
- Case 2 : non critical values that I'm using more for debug and I don't care to loose in case of reboot

For those case, I feel that using `context.xxx()` API is a bit "too much" and remove the flexibility of using the values directly as variables/members without having the seed to call for `get()` and `set()`

Here is a simple message counter exempl where I solved the situation using `this.`  
This is what I meant by "simple variables". Something that I can use directly and not through API calls  
Still I don't understand why it is enough to use `this.` only once in "On Start" and once in "On Message" and not every time.

```auto
[{"id":"3ae3f90e6c815dd6","type":"function","z":"bbbd97e84cc4a26b","name":"counter","func":"if (msg.reset) {\n this.counter = 0;\n node.status({ fill: \"blue\", shape: \"dot\", text: `${counter}` })\n return null;\n}\ncounter = counter + 1\nnode.status({ fill: \"blue\", shape: \"dot\", text: `${counter}` })\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nthis.counter = 0\nnode.status({ fill: \"blue\", shape: \"dot\", text: `${counter}` })\n","finalize":"","libs":[],"x":440,"y":7220,"wires":[["a299c77d20d6cf41"]]},{"id":"279a9df6434c4ce7","type":"inject","z":"bbbd97e84cc4a26b","name":"count","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":7200,"wires":[["3ae3f90e6c815dd6"]]},{"id":"be8c87b4ab52342a","type":"inject","z":"bbbd97e84cc4a26b","name":"reset","props":[{"p":"reset","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":7240,"wires":[["3ae3f90e6c815dd6"]]},{"id":"a299c77d20d6cf41","type":"debug","z":"bbbd97e84cc4a26b","name":"debug 211","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":7220,"wires":[]}]

```

I would be pleased to get your review and comments

Thanks

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [2 January 2024 20:52 UTC](https://discourse.nodered.org/t/variable-between-on-start-and-on-message/84216/4 "2024-01-02T20:52:15Z")

</div>

> [@Barbudor](#):
>
> Still I don't understand why it is enough to use `this.` only once in "On Start" and once in "On Message" and not every time.

Well. It looks like you don't need to use `this.` at all. Try it & ignore the complaints when deploying as well as the red triangle. Your flow runs as you intended.

Why does it run? Node-RED re-uses the vm (to be precise the vm _context_) to run the scripts of the `function` node. Looks like the definition in "OnStart" thus gets forwarded to "OnMessage".

This might be convenient - I'm yet not convinced that this was the idea of operation. Why? The syntax checker treats the scripts as separate, not-linked scopes.

Those two aspects don't match...

👉 @Steve-Mcl

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [2 March 2024 20:52 UTC](https://discourse.nodered.org/t/variable-between-on-start-and-on-message/84216/5 "2024-03-02T20:52:43Z")

</div>

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