# Why am I getting NaN?

**URL:** <https://discourse.nodered.org/t/why-am-i-getting-nan/27869>\
**Category:** General\
**Created:** [5 June 2020 22:30 UTC](https://discourse.nodered.org/t/why-am-i-getting-nan/27869 "2020-06-05T22:30:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [5 June 2020 22:30 UTC](https://discourse.nodered.org/t/why-am-i-getting-nan/27869/1 "2020-06-05T22:30:55Z")

</div>

I am trying to write a function to give me a running average of a sensor value, but no matter what I do, the output is NaN. Yes, I did discover the Mean Value node, so my math is superfluous. Except, I would like to know why I am getting a NaN output- just to learn what I did wrong.

```auto
//Put just the average moisture into the payload.
var moistureRaw = msg.moistureRaw;
var runningWet = node.get('runningWet') || 0;
runningWet = (runningWet + moistureRaw ) /2;
node.set('runningWet', runningWet);
msg.payload = runningWet;
return msg;

```

---

<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:** [5 June 2020 22:35 UTC](https://discourse.nodered.org/t/why-am-i-getting-nan/27869/2 "2020-06-05T22:35:59Z")

</div>

```auto
node.get('runningWet')

```

There is no `node.get` function. If you are trying to use the node-scoped context, then use `context.get/set`.

But I would have expected that to result in an obvious error message in the Debug sidebar telling you `node.get` is not a function.

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [6 June 2020 02:20 UTC](https://discourse.nodered.org/t/why-am-i-getting-nan/27869/3 "2020-06-06T02:20:56Z")

</div>

Actually, my code uses globals, but I changed it at the last moment before posting the question because I knew someone would rail on me for using global.get and global.set for variables not needed in another flow.

So other than that foopas', why is my function sending a NaN? Like I said, the Mean Value node took care of my need to average the sensor readings, but I would like to know what's wrong with my code? (For future knowledge).

```auto
//Put just the average moisture into the payload.
var moistureRaw = msg.moistureRaw;
var runningWet = global.get('runningWet') || 0;
runningWet = (runningWet + moistureRaw ) /2;
global.set('runningWet', runningWet);
msg.payload = runningWet;
return msg;

```

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [6 June 2020 05:48 UTC](https://discourse.nodered.org/t/why-am-i-getting-nan/27869/4 "2020-06-06T05:48:57Z")

</div>

It happens if msg.moistureRaw is undefined

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [6 June 2020 13:13 UTC](https://discourse.nodered.org/t/why-am-i-getting-nan/27869/5 "2020-06-06T13:13:58Z")

</div>

You are correct. On a previous node I had moved msg.moistureRaw to payload.

Would this:  
`var moistureRaw = msg.moistureRaw || 0;`  
resolve an unidentified variable and prevent the NaN error?

(Not for this function, but for future reference).

---

<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:** [6 June 2020 13:20 UTC](https://discourse.nodered.org/t/why-am-i-getting-nan/27869/6 "2020-06-06T13:20:27Z")

</div>

> [@SteveMann](#):
>
> Would this:  
> `var moistureRaw = msg.moistureRaw || 0;`  
> resolve an unidentified variable and prevent the NaN error?

It would with the important caveat that it would only work as you expect with a 0 on the right hand side.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [6 June 2020 14:11 UTC](https://discourse.nodered.org/t/why-am-i-getting-nan/27869/7 "2020-06-06T14:11:58Z")

</div>

There is a function to check whether a value is NaN:

[https://www.w3schools.com/jsref/jsref\_isnan.asp](https://www.w3schools.com/jsref/jsref_isnan.asp)

---

<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:** [5 August 2020 14:12 UTC](https://discourse.nodered.org/t/why-am-i-getting-nan/27869/8 "2020-08-05T14:12:06Z")

</div>

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