# Error in a function in Node-Re 2.2 not present in previous versoins

**URL:** <https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357>\
**Category:** General\
**Created:** [27 January 2022 17:42 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357 "2022-01-27T17:42:53Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [28 January 2022 10:22 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/21 "2022-01-28T10:22:40Z")

</div>

The setup funnction with condition removed

```auto
//if (!timing || !Array.isArray(timing)) // new
//if ( typeof context.global.timing == 'undefined' ) 
    {
       //context.global.timing=[
       global.set("timing",[
                        14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
                        14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
                        14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
                        14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
                        14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
                        14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
                        14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
                        8,14
                        ]);
        context.saving=0;
        msg.payload="anything"; msg.foryou="Restoring"; return([null,null,msg]);
    }

var timing=global.get("timing");

```

and the "process heat" function in total

```auto
var timing=global.get("timing");
node.warn(["timing is -->", timing]);
var now = new Date();
msg.payload=timing[(now.getDay()*24)+now.getHours()];
node.warn(["msg.payload is now -->", msg.payload]);
msg.away=timing[169];

msg.temperature=flow.get("incomingTemperature");
node.warn(["msg.temperature is -->", msg.temperature]);
msg.humidity=flow.get("incomingHumidity");
msg.set=msg.payload;
if (flow.get("manualTimer")) flow.set("manualTimer",flow.get("manualTimer")-1);
else flow.set("manual",0);
msg.desired=msg.set;
flow.set("setTemperature",msg.desired);
msg.desired+=flow.get("manual");
// fixed local away to global away on jan 17 2020
if (global.get("away")) msg.desired=global.get("timing")[169];  
if (msg.desired>msg.temperature) { msg.relay=1; } else { msg.relay=0; }

msg.payload=flow.get("incomingTemperature");

node.status({fill:"blue",shape:"dot",text:"Set point " + msg.desired + "c - Actual " + msg.temperature +"c"});

msg.topic="processHeat";
if (msg.temperature<msg.desired) msg.colour="#ff5555";
node.send([msg,null]);
msg.payload=msg.relay;
node.send([null,msg]);

```

---

<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:** [28 January 2022 10:29 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/22 "2022-01-28T10:29:26Z")

</div>

Something else in your flow must deleting global.timing be an issue because your version of the function DOES set global.timing...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4dad564e79e340c97c9422b5f816cadaae57f30c.png)

and I can retrieve it...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/27dec7183495b38537169327a3221823937d2580.png)

Are you using a change node or another function somewhere to update `global.timing`?

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [28 January 2022 10:44 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/23 "2022-01-28T10:44:18Z")

</div>

Your marker that said it wsan't going anywhere - after thah code in the startup function, I do some checks on timing. As for PROCESS HEAT you have the whole function.  
i put up your conext viewer and JUST after manually triggering the init you see I hit refresh in the relevant conbtext viewer part and still no sign of timing...

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 January 2022 10:55 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/24 "2022-01-28T10:55:46Z")

</div>

Peter,  
How have you defined 'context storage' in settings.js? (if you look at your startup log you should see something like this:

```auto
28 Jan 05:39:37 - [info] Context store : 'default' [module=localfilesystem]

```

---

<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:** [28 January 2022 11:04 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/25 "2022-01-28T11:04:49Z")

</div>

Peter, can you do a couple of basic tests & show me the results please...

![chrome_AIi0y5Y8op](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/4705c45e3ea2ef59cc4e01870bf705cb7abc8c0e.gif)

```auto
[{"id":"43a432396bcb5e59","type":"inject","z":"950e6f56f9415563","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2500,"y":360,"wires":[["0fcd5312e67aa420"]]},{"id":"0fcd5312e67aa420","type":"function","z":"950e6f56f9415563","name":"set flow.test","func":"flow.set(\"test\", (new Date()).toISOString())\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2680,"y":360,"wires":[["4ae9e8044fa3e804"]]},{"id":"4ae9e8044fa3e804","type":"function","z":"950e6f56f9415563","name":"set global.test","func":"global.set(\"test\", (new Date()).toISOString())\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2690,"y":400,"wires":[["4a7a3179e32066c9"]]},{"id":"4a7a3179e32066c9","type":"change","z":"950e6f56f9415563","name":"flow.test2","rules":[{"t":"set","p":"test2","pt":"flow","to":"$now()","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":2670,"y":440,"wires":[["8d7f9a8bedc3824c"]]},{"id":"8d7f9a8bedc3824c","type":"change","z":"950e6f56f9415563","name":"global.test2","rules":[{"t":"set","p":"test2","pt":"global","to":"$now()","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":2680,"y":480,"wires":[["13ad3631ca929c25"]]},{"id":"13ad3631ca929c25","type":"function","z":"950e6f56f9415563","name":"node warn","func":"var x = {\n \"flow.test\": global.get(\"test\"),\n \"global.test\": global.get(\"test\"),\n \"flow.test2\": flow.get(\"test2\"),\n \"global.test2\": global.get(\"test2\"),\n}\nnode.warn(x)\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2710,"y":540,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [28 January 2022 11:30 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/26 "2022-01-28T11:30:45Z")

</div>

Sorry - I've been working with heating engineers servicing our heating - just as I'm messing here with the thrermostat. I'm back and will read this now.

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [28 January 2022 11:33 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/27 "2022-01-28T11:33:30Z")

</div>

Does this help - my /home/pi/.node-red/settings.js - relevant part

```auto
     contextStorage: {
    	default : { module: "localfilesystem"}
    },

```

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [28 January 2022 11:36 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/28 "2022-01-28T11:36:49Z")

</div>

Steve

I imported your code into the flow  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/5/95979cc8c637fab8b2d3b355ff650d084ab69477.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3c534a6e08d5b459cfc7dc2a13689cc13712fbd7.png)

And the GLOBAL is historical - the array is not used outside of this page (flow)

Test and test2 appear in globals on refresh - they also appear in flow

But there is no sign of my TIMING array in globals - yet this has worked for years..

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [28 January 2022 11:47 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/29 "2022-01-28T11:47:01Z")

</div>

If this helps - the entire flow..

```auto
[{"id":"43a432396bcb5e59","type":"inject","z":"950e6f56f9415563","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2500,"y":360,"wires":[["0fcd5312e67aa420"]]},{"id":"0fcd5312e67aa420","type":"function","z":"950e6f56f9415563","name":"set flow.test","func":"flow.set(\"test\", (new Date()).toISOString())\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2680,"y":360,"wires":[["4ae9e8044fa3e804"]]},{"id":"4ae9e8044fa3e804","type":"function","z":"950e6f56f9415563","name":"set global.test","func":"global.set(\"test\", (new Date()).toISOString())\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2690,"y":400,"wires":[["4a7a3179e32066c9"]]},{"id":"4a7a3179e32066c9","type":"change","z":"950e6f56f9415563","name":"flow.test2","rules":[{"t":"set","p":"test2","pt":"flow","to":"$now()","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":2670,"y":440,"wires":[["8d7f9a8bedc3824c"]]},{"id":"8d7f9a8bedc3824c","type":"change","z":"950e6f56f9415563","name":"global.test2","rules":[{"t":"set","p":"test2","pt":"global","to":"$now()","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":2680,"y":480,"wires":[["13ad3631ca929c25"]]},{"id":"13ad3631ca929c25","type":"function","z":"950e6f56f9415563","name":"node warn","func":"var x = {\n \"flow.test\": global.get(\"test\"),\n \"global.test\": global.get(\"test\"),\n \"flow.test2\": flow.get(\"test2\"),\n \"global.test2\": global.get(\"test2\"),\n}\nnode.warn(x)\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2710,"y":540,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [28 January 2022 11:58 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/30 "2022-01-28T11:58:11Z")

</div>

Of interest the array is stored in a file -  
and I just checked - that file is still intact with my settings.

```auto
[20,19,19,19,19,19,20,20,21,21,21,21,21,21,21,21,21,21,22,22,22,22,22,20,20,19,19,19,19,19,20,20,21,21,21,21,21,21,21,21,21,21,22,22,22,22,22,20,20,19,19,19,19,19,20,20,21,21,21,21,21,21,21,21,21,21,22,22,22,22,22,20,20,19,19,19,19,19,20,20,21,21,21,21,21,21,21,21,21,21,22,22,22,22,22,20,20,19,19,19,19,19,20,20,21,21,21,21,21,21,21,21,21,21,22,22,22,22,22,20,20,19,19,19,19,19,20,20,21,21,21,21,21,21,21,21,21,21,22,22,22,22,22,20,20,19,19,19,19,19,20,20,21,21,21,21,21,21,21,21,21,21,22,22,22,22,22,20,8,12]

```

---

<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:** [28 January 2022 12:25 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/31 "2022-01-28T12:25:43Z")

</div>

> [@scargill](#):
>
> Test and test2 appear in globals on refresh

This proves your context storage is working - good.

That means something else (possibly in your flow) is destroying the global.timing array.

---

<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:** [28 January 2022 12:26 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/32 "2022-01-28T12:26:49Z")

</div>

Interesting

> [@scargill](#):
>
> that file is still intact with my settings.
> 
> ```auto
> [20,19,19,19,19,19,20,20,21,21,2....
> 
> ```

Interesting.

Would you be able to share that file (private message if need be)

---

<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:** [28 January 2022 12:27 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/33 "2022-01-28T12:27:47Z")

</div>

> [@scargill](#):
>
> If this helps - the entire flow..

Unfortunately, that is just the test flow I sent you ☹

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 January 2022 12:29 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/34 "2022-01-28T12:29:50Z")

</div>

I think that you will need to search your flows for `global.set("timing"` and see if there are any other references.

I think that I would also rename that variable to be more descriptive. That might also drive out some other references to it that you may have overlooked.

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [28 January 2022 12:39 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/35 "2022-01-28T12:39:02Z")

</div>

That would be because between us we had var timing and let timing. I'm assuming LET is a reference to the global sa against a local version? It SEEMS now to work... I've left LET in place and commened out the VAR version..

The stat SEEMS to be working... and thanks for your efforts guys...

Is LET a copy or does it point to the original? Never used that before.

---

<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:** [28 January 2022 12:43 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/36 "2022-01-28T12:43:47Z")

</div>

> [@scargill](#):
>
> That would be because between us we had var timing and let timing. I'm assuming LET is a reference to the global sa against a local version

`let` is (almost) the same as `var` (i wont get into the finer details like scoping or hoisting) but let me simplify it further by offering you this golden rule...

- Use `const` for everything
- Use `let` when `const` doesn't work
- Dont use `var`

> [@scargill](#):
>
> The stat SEEMS to be working... and thanks for your efforts guys...

Happy to help.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 January 2022 12:51 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/37 "2022-01-28T12:51:28Z")

</div>

I think that it is worth pointing out that the difference between let/const and var _can_ easily catch you out.

let/const only work from ES6 onwards. But that is all currently supported versions of node.js

let/const is position sensitive. var on the other hand is not, JavaScript will "hoist" a var to the top-level context of your function. But let/const are restricted in context to the parent code block and must be declared before use. Also, you can re-declare a variable using let/const within a child code block, you cannot do that with a var.

For the most part, most people ignore the differences but it WILL eventually catch you out 🙂

So as Steve says, generally don't use var. But, there are still a few cases where the limited context of let/const is difficult or annoying to deal with and it is just easier to use a var.

In the browser, things are different - if you have to support old browsers. Anything old enough not to support ES6 requires only the use of var. So old iPads or Android tablets for example that you may not be able to upgrade.

---

<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:** [28 January 2022 13:01 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/38 "2022-01-28T13:01:59Z")

</div>

> [@Steve-Mcl](#):
>
> `let` is (almost) the same as `var` (i wont get into the finer details like scoping or hoisting)

But Julian will 😃

Well written Julian - I just didn't want to be the one to raise his blood pressure.

  

I'll amend my earlier statement...

- In node / function node
  - Use `const` for everything
  - Use `let` when `const` doesn't work
  - Try to avoid using `var`

- In browser/client side code (ui template etc)
  - You will be forgiven for using `var` 🙂

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [29 January 2022 08:22 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/39 "2022-01-29T08:22:26Z")

</div>

hi

Perhhaps I could ask one of you clever chaps for a last clarification. Oh and my blood pressure is chemically controlled so no worries 🙂

The reason I used "var timing" was laziness - to avoid having to key in clobal.get etc every time - so timeing was a temporary local copy of the array (I hope). Can you clarify:

```auto
let timing=global.get("timing")

```

Am I now working with a copy of the original global of is "timing" the actual global? If I'm working with the global I'm more than happy but I'd like to be sure - and thanks once again for helping. I'm no-where near a node-red newbie but like most self-taught, I have the odd gap in my knowledge and clarification will hopefully clear it up.

Regards

Pete

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [29 January 2022 08:47 UTC](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357/40 "2022-01-29T08:47:47Z")

</div>

> [@scargill](#):
>
> Am I now working with a copy of the original global of is "timing" the actual global?

It returns a reference to the object in context, not a copy.

If you want to modify it without changing in context, you will need to clone it first.

As you know it's an array, you could do:

```auto
let timing = [...global.get("timing")]

```

Or, more generally

```auto
let timing = RED.util.cloneMessage(global.get("timing"))

```

[Previous page](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357.md?page=1)

[Next page](https://discourse.nodered.org/t/error-in-a-function-in-node-re-2-2-not-present-in-previous-versoins/57357.md?page=3)
