# NR complains about incorrect javascript code - but its not

**URL:** https://discourse.nodered.org/t/nr-complains-about-incorrect-javascript-code-but-its-not/54916
**Category:** General
**Tags:** function-node
**Created:** [9 December 2021 09:01 UTC](https://discourse.nodered.org/t/nr-complains-about-incorrect-javascript-code-but-its-not/54916 "2021-12-09T09:01:49Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)
#### Post date: [9 December 2021 09:01 UTC](https://discourse.nodered.org/t/nr-complains-about-incorrect-javascript-code-but-its-not/54916/1 "2021-12-09T09:01:49Z")

</div>

I got these lines in a function to check for the presence of a sensor:

```auto
var t = new Date(msg.payload.attr.lastseen);
var t0 = new Date();
nmsg.payload.diff = (t0 - t) / 1000;

```

So .diff is the time difference in seconds. In Javascript you should be able to do some arithmetic operations with Date object. However NR is complaining about a type error in this function node:

```auto
The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.(2362)

```

Actually the function works as expected but can I convince NR that this code is correct?

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [9 December 2021 09:16 UTC](https://discourse.nodered.org/t/nr-complains-about-incorrect-javascript-code-but-its-not/54916/2 "2021-12-09T09:16:25Z")

</div>

Hello Haegar,

read this thread that i think is related

> [@Monaco editor does not allow subraction of dates](https://discourse.nodered.org/t/monaco-editor-does-not-allow-subraction-of-dates/53052):
>
> I am not sure where to report this. When using the Monaco editor this code, in a function node, generates an error. const d1 = new Date() const d2 = new Date(d1.getTime() + 1000) msg.payload = d2 - d1 return msg; Hovering over d2 in the third line shows: [image]

---

<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: [9 December 2021 09:25 UTC](https://discourse.nodered.org/t/nr-complains-about-incorrect-javascript-code-but-its-not/54916/3 "2021-12-09T09:25:09Z")

</div>

You can either provide in-line type overrides to force Monaco to think that the dates are numbers. Or you can force the dates to numbers when you create them. My response in that other thread shows you how.

---

<div class="post-metadata">

### Author: ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)
#### Post date: [9 December 2021 12:01 UTC](https://discourse.nodered.org/t/nr-complains-about-incorrect-javascript-code-but-its-not/54916/4 "2021-12-09T12:01:45Z")

</div>

Thanks, that thread was helpful. Simple double brackets did not help but a construction like

```auto
var t = Date.parse(msg.payload.lastupdated);
var t0 = Number(new Date());

```

did the job 😉  
I hope this bug in the Monaco editor is corrected sometime..

---

<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: [9 December 2021 12:09 UTC](https://discourse.nodered.org/t/nr-complains-about-incorrect-javascript-code-but-its-not/54916/5 "2021-12-09T12:09:24Z")

</div>

> [@haegar33](#):
>
> `var t0 = Number(new Date());`

Use `Date.now()` instead of constructing a date object and it will already be a number (epoch)

Also, use `t.valueOf()` to get the epoch from your parsed date.

> [@haegar33](#):
>
> I hope this bug in the Monaco editor is corrected sometime.

I'm not sure it's a bug. The description is gives is correct.

---

<div class="post-metadata">

### Author: ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)
#### Post date: [9 December 2021 12:46 UTC](https://discourse.nodered.org/t/nr-complains-about-incorrect-javascript-code-but-its-not/54916/6 "2021-12-09T12:46:50Z")

</div>

> [@Steve-Mcl](#):
>
> I'm not sure it's a bug. The description is gives is correct.

A parser which reports a syntax error (and not a warning) which is not true is definitely a bug, what else? It leads to a NodeRed complain message everytime you deploy a flow.  
But I have seen the discussion on this on the other thread.

---

<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: [9 December 2021 13:54 UTC](https://discourse.nodered.org/t/nr-complains-about-incorrect-javascript-code-but-its-not/54916/7 "2021-12-09T13:54:13Z")

</div>

> [@Steve-Mcl](#):
>
> I'm not sure it's a bug. The description is gives is correct.

That's correct, it isn't a bug. Monaco is correct, it is because JavaScript isn't strongly typed and that can result in incorrect processing of date objects.

> [@haegar33](#):
>
> A parser which reports a syntax error (and not a warning) which is not true is definitely a bug

The error comes from Node-RED's treatment of Monaco. The warning from the type checker in Monaco comes from the Typescript engine and in typescript misuse of a date object IS an error. It is just that JavaScript natively lets you get away with it. Doesn't make it right. 🙂

---

<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: [23 December 2021 13:54 UTC](https://discourse.nodered.org/t/nr-complains-about-incorrect-javascript-code-but-its-not/54916/8 "2021-12-23T13:54:51Z")

</div>

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