# Arithmetic operation \[2362\] error on one node-red but not other

**URL:** https://discourse.nodered.org/t/arithmetic-operation-2362-error-on-one-node-red-but-not-other/85833
**Category:** Developing Nodes
**Tags:** function-node
**Created:** [23 February 2024 20:00 UTC](https://discourse.nodered.org/t/arithmetic-operation-2362-error-on-one-node-red-but-not-other/85833 "2024-02-23T20:00:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![nzbaxterman](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzbaxterman/32/80328_2.png) [@nzbaxterman](https://discourse.nodered.org/u/nzbaxterman)
#### Post date: [23 February 2024 20:00 UTC](https://discourse.nodered.org/t/arithmetic-operation-2362-error-on-one-node-red-but-not-other/85833/1 "2024-02-23T20:00:23Z")

</div>

Hello  
I'm getting a javascript warnings when deploying on one Nodered but not another.

In it's most simplest form, the function code is as follows:

```auto
var temp = context.get("last_update"); //create a node variable
var current = new Date(); //capture the current date
msg.payload = current - temp; //set the payload to the time date of the last update
context.set("last_update",current); //save the node variable

```

(Obviously within the node I'm doing more, this is just for demonstration purposes)

On one NodeRed this Function compiles without warnings or errors.  
But on my RPi-4 Node Red I get a warning about the line starting "msg.payload" saying:  
"The workspace contains some nodes that are not properly configured"  
"The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2362)"

Can anyone help?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [23 February 2024 20:45 UTC](https://discourse.nodered.org/t/arithmetic-operation-2362-error-on-one-node-red-but-not-other/85833/2 "2024-02-23T20:45:58Z")

</div>

Welcome to the forum @nzbaxterman.

> [@nzbaxterman](#):
>
> msg.payload = current - temp  
> The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2362)

The arithmetic expression in question is `current - temp` so the left hand side is `current`  
current is a date object, not one of the permitted datatypes.  
try `current.getTime() - temp.getTime()`

You might need to set a default value in your context.get statement too for the first time the function is run after a deploy  
`var temp = context.get("last_update") || new Date()` might work.

---

<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: [23 February 2024 21:00 UTC](https://discourse.nodered.org/t/arithmetic-operation-2362-error-on-one-node-red-but-not-other/85833/3 "2024-02-23T21:00:49Z")

</div>

This is caused by Monaco's Typescript engine being a numpty (something it excels at sadly).

The quickest fix is to declare the type:

```auto
const temp = context.get("last_update"); //create a node variable
/** @type {number} */
const current = new Date(); //capture the current date
msg.payload = current - temp; //set the payload to the time date of the last update
context.set("last_update",current); //save the node variable

```

Alternately, you could actually cast the date to a number:

```auto
const temp = context.get("last_update"); //create a node variable
const current = new Date(); //capture the current date
msg.payload = Number(current) - temp; //set the payload to the time date of the last update
context.set("last_update",current); //save the node variable

```

That also works but is a tiny bit more work for the node.

Also note that I've used `const` instead of `var`, you should ideally read up on the differences between `var`, `let` and `const`. The use of `var` is generally discouraged in JavaScript since it can lead to subtle bugs that are hard to track down.

And finally take note of jbudd's recommendation on the `context.get` line, it is best to give it a default just in case the variable is unset or something else that cannot be cast as a number. Though I would personally use the newer format:

```auto
const temp = context.get("last_update") ?? new Date()

```

Which will avoid issues should your context var ever contain zero.

Of course, defaulting to the current date/time might not be what you need and you might even want to wrap your code in an if so that it doesn't process if `last_update` is not yet set.

---

<div class="post-metadata">

### Author: ![nzbaxterman](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nzbaxterman/32/80328_2.png) [@nzbaxterman](https://discourse.nodered.org/u/nzbaxterman)
#### Post date: [24 February 2024 04:45 UTC](https://discourse.nodered.org/t/arithmetic-operation-2362-error-on-one-node-red-but-not-other/85833/4 "2024-02-24T04:45:49Z")

</div>

OK thanks - I now need to share more of the function node which throws the same compile warning:

This Deploys OK, but has the same warning:

```auto
var temp = context.get("last_update") || new Date();
var current = new Date();

if (temp!==undefined) {
    current = current.getTime() - temp.getTime();
    current = Math.floor(current/1000);
    msg.secsincelast = current;
    var minute = Math.floor(current/60);
    var hour = Math.floor(minute/60);
    var day = Math.floor(hour/24);
    if (current>=24*60*60) {
        msg.lastupdate = "Last update " + day + " days, " + hour%24 + " hours, " + minute%60 + " minutes, " + current%60 + " seconds ago";
    } else if (current>=60*60) {
        msg.lastupdate = "Last update " + hour%24 + " hours, " + minute%60 + " minutes, " + current%60 + " seconds ago";
    } else if (current>=60) {
        msg.lastupdate = "Last update " + minute%60 + " minutes, " + current%60 + " seconds ago";
    } else {
        msg.lastupdate = "Last update " + current%60 + " seconds ago";
    }
}

```

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [24 February 2024 05:52 UTC](https://discourse.nodered.org/t/arithmetic-operation-2362-error-on-one-node-red-but-not-other/85833/5 "2024-02-24T05:52:01Z")

</div>

The editor complains because

```auto
var temp = context.get("last_update") || new Date();
var current = new Date();

```

At this point `current` is a Date object

```auto
if (temp!==undefined) {
    current = current.getTime() - temp.getTime();
    current = Math.floor(current/1000);

```

And here you are assigning an integer value to it (the difference in milliseconds)

If you were to change it to this it might work (not tested)

```auto
var temp = context.get("last_update") || new Date();
var current = new Date();

if (temp!==undefined) {
    var diff = current.getTime() - temp.getTime();
    diff = Math.floor(diff/1000);
    msg.secsincelast = diff;
    var minute = Math.floor(diff/60);
    var hour = Math.floor(minute/60);
    var day = Math.floor(hour/24);
    if (diff>=24*60*60) {
        msg.lastupdate = "Last update " + day + " days, " + hour%24 + " hours, " + minute%60 + " minutes, " + diff%60 + " seconds ago";
    } else if (diff>=60*60) {
        msg.lastupdate = "Last update " + hour%24 + " hours, " + minute%60 + " minutes, " + diff%60 + " seconds ago";
    } else if (diff>=60) {
        msg.lastupdate = "Last update " + minute%60 + " minutes, " + diff%60 + " seconds ago";
    } else {
        msg.lastupdate = "Last update " + diff%60 + " seconds ago";
    }
}

```

---

<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: [24 February 2024 12:17 UTC](https://discourse.nodered.org/t/arithmetic-operation-2362-error-on-one-node-red-but-not-other/85833/6 "2024-02-24T12:17:39Z")

</div>

The same principals apply that I explained in my last post. Go through the code and either cast the dates to numbers or add `@type` comments and the "errors" will go away.

---

<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: [24 April 2024 12:18 UTC](https://discourse.nodered.org/t/arithmetic-operation-2362-error-on-one-node-red-but-not-other/85833/7 "2024-04-24T12:18:08Z")

</div>

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