# What am I doing wrong

**URL:** <https://discourse.nodered.org/t/what-am-i-doing-wrong/9513>\
**Category:** General\
**Created:** [27 March 2019 22:17 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513 "2019-03-27T22:17:40Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)\
**Post date:** [1 April 2019 21:06 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/21 "2019-04-01T21:06:59Z")

</div>

So, this is my current code.

```auto
if (global.get("FSwitch") === "Off"){
    
    var Fan = (global.get("FWattage"));
    var Fan={};
    
        }
if (global.get("PSwitch") === "Off");{
    
    var Pump= (global.get("PWattage"));
    var Pump={};
    
}

Fan.payload= Fan;
Pump.payload= Pump;

return [Fan, Pump];

```

And this is the payload I get.

```auto
msg : Object
object
payload: "[Circular ~]"
_msgid: "2237cea5.774e22"

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 April 2019 21:26 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/22 "2019-04-01T21:26:50Z")

</div>

Why are you assigning the variables twice ? you set Pump then set it again to an empty object... ???

(Hmm - that is copied from Steve's code above - - not sure what he was thinking... )

Maybe this instead

```auto
var Fan;
var Pump;
if (global.get("FSwitch") === "Off"){
    Fan = { payload : global.get("FWattage") };
}
if (global.get("PSwitch") === "Off");{
    Pump = { payload: global.get("PWattage") };
}
return [Fan, Pump];

```

---

<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:** [2 April 2019 04:00 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/23 "2019-04-02T04:00:16Z")

</div>

> [@rburton](#):
>
> I've been to w3schools and its basic syntax covered, and to be honest the wiki pages are, to be honest for a beginner confusing (Node-Red)

Clearly, I did really meant this as a kind suggestion, you definitely need it

---

<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 April 2019 07:09 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/24 "2019-04-02T07:09:18Z")

</div>

> [@dceejay](#):
>
> (Hmm - that is copied from Steve's code above - - not sure what he was thinking... )

Haha. Didn't even spot that. I was fixing up the `if` statement and showing he didn't need 2 outputs. Doh!

@rburton I don't mean to be mean here but you really really need to do some JS tutorials. There are so many school boy errors in your code that don't make any sense at all.

Good luck though.

Steve.

---

<div class="post-metadata">

**Author:** ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)\
**Post date:** [2 April 2019 20:58 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/25 "2019-04-02T20:58:55Z")

</div>

Thanks for the inputs, ive only been doing it for the last three weeks, and in the process of a masters degree, as for reading tutorials and watching videos not all are usefull if i dont understand the errors im making, that is why im asking the questions useful answers are apprieciated.

---

<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:** [2 April 2019 21:17 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/26 "2019-04-02T21:17:06Z")

</div>

Yeah, you are lucky, we are not going to decide if you pass your examination, the methods you use here will not work in mathematics & physics

---

<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:** [2 April 2019 23:40 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/27 "2019-04-02T23:40:34Z")

</div>

Unless you haven't touched the function node from the initial post (in which case you should state that), you need to provide it again so people can see what you have now cobbled together.

---

<div class="post-metadata">

**Author:** ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)\
**Post date:** [3 April 2019 06:18 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/28 "2019-04-03T06:18:46Z")

</div>

Hello, @zenofmud post 20 was the reconfigure post.  
But now gone with @dceejay reply but with that, if the switch goes to on fan stops passing a msg, but pump is still returning a msg even when that global is switched to on.

---

<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:** [3 April 2019 08:56 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/29 "2019-04-03T08:56:11Z")

</div>

So

1. what is the data going into the function? Is a debug node on the node feeding the function.
2. where are the globals first initialized
3. copy and paste the function node so it can be examined to make sure you entered it correct (you wouldn’t believe how many times I’ve had a typo)

---

<div class="post-metadata">

**Author:** ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)\
**Post date:** [7 April 2019 15:05 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/30 "2019-04-07T15:05:06Z")

</div>

Thanks @zenofmud it was indeed a typo.

I've now changed to suit what I need, Flow.get 1,2,3,4 are as follows and set to numbers.

![2019-04-07%2016_01_34-Window](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9d999aab38b56d855be311318428d500cb18d099.png)

And with the following code I'm getting a NAN (not a number)

```auto
var Fan;
var Pump;
var PowerH;
var APump;
var T = Fan+Pump+PowerH+APump;

if (global.get("FSwitch") === "On"){
    Fan = { payload : flow.get("4")};
}else{
    Fan = { payload : 0};
}
if (global.get("PSwittch") === "On"){
    Pump = { payload: flow.get("1") };
}else{
    Pump = { payload : 0};
}
if (global.get("PHSwitch") === "On"){
    PowerH = { payload: flow.get("2") };
}else{
    PowerH = { payload : 0};
}
if (global.get("APSwitch") === "On"){
    APump = { payload: flow.get("3") };
}else{
    APump = { payload : 0};
}

//return [Fan, Pump, PowerH, APump];
msg.payload = (T);
return msg;

```

what is the reason for getting NaN?

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [7 April 2019 15:19 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/31 "2019-04-07T15:19:23Z")

</div>

You declare this first;

> [@rburton](#):
>
> `var Fan; var Pump; var PowerH; var APump; var T = Fan+Pump+PowerH+APump;`

Then don't do anything with T until the end where you do

```auto
//return [Fan, Pump, PowerH, APump];
msg.payload = (T);

```

But for one thats not what you are returning as you made T a variable.  
and I'm not sure what the brackets do...

Take a look at the logging events section of the function docs [Redirecting…](https://nodered.org/docs/writing-functions#logging-events)

If you don't get a result you expect, try adding `node.warn` lines to your code  
eg`node.warn("T at the the end of the code is:"+T);` to see where it is going wrong...

---

<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:** [7 April 2019 16:23 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/32 "2019-04-07T16:23:08Z")

</div>

Once again, this is basic javascript know-how & understanding. You can't just declare the variable and then expect that it will get the value "from heaven"

Do like this and notice the difference and reason for the changed code

```auto
var Fan;
var Pump;
var PowerH;
var APump;

if (global.get("FSwitch") === "On"){
    Fan = { payload : flow.get("4")};
}else{
    Fan = { payload : 0};
}
if (global.get("PSwittch") === "On"){
    Pump = { payload: flow.get("1") };
}else{
    Pump = { payload : 0};
}
if (global.get("PHSwitch") === "On"){
    PowerH = { payload: flow.get("2") };
}else{
    PowerH = { payload : 0};
}
if (global.get("APSwitch") === "On"){
    APump = { payload: flow.get("3") };
}else{
    APump = { payload : 0};
}

//return [Fan, Pump, PowerH, APump];
var T = Fan+Pump+PowerH+APump;
msg.payload = T;
return msg;

```

---

<div class="post-metadata">

**Author:** ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)\
**Post date:** [7 April 2019 16:50 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/33 "2019-04-07T16:50:09Z")

</div>

Once again, @krambriw you input is appreciated, but your approach is not, maybe you own this forum but I doubt that, and you only want to see the correct answer posted all the time.  
Let me just enlighten you @krambriw a forum is a place for people to come and learn find information on questions they may otherwise struggle with, I'm sure there are things that you are incapable of understanding outside of the realm of JavaScript, and hope you never need to approach a forum to get shot down by a troll like yourself.

Maybe from now on do not post on my topics!!

---

<div class="post-metadata">

**Author:** ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)\
**Post date:** [7 April 2019 16:54 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/34 "2019-04-07T16:54:26Z")

</div>

Thanks @ukmoose I'm away from my pc ATM but will give that ago when I'm back on, and as for the brackets they wasnt meant to be there I'd adding them trying something I'd read on a forum but was incorrect.

---

<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:** [7 April 2019 19:13 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/35 "2019-04-07T19:13:21Z")

</div>

Not entirely certain of the end goal (too much noise, errors and mis-information in this thread) however the based on the suggestion by @krambriw i have made an educated guess. I will provide my solution at the end.

**But first and most importantly...**  
In the interest of helping you to help yourself and help you understand why people keep saying you should perhaps learn JavaScript, PLEASE read and understand the errors in your code below...

**Your original code - please read my comments...**

> [@rburton](#):
>
> And with the following code I'm getting a NAN (not a number)
> 
> ```auto
> / ***FIRST: When a var is created, it will be == undefined.*** /
> var Fan; //INFO: This will be undefined. You should initialise the var here with a default value
> var Pump; //INFO: This will be undefined. You should initialise the var here with a default value
> var PowerH;//INFO: This will be undefined. You should initialise the var here with a default value
> var APump;//INFO: This will be undefined. You should initialise the var here with a default value
> 
> var T = Fan+Pump+PowerH+APump; //BUG: You are attempting to add 4 undefined's!!!!.
> /* AT THIS POINT T will be Nan see proof link below*/
> 
> //next you go on to create 4 msgs with a payload loaded from flow (if == "On")
> //however you dont use them
> if (global.get("FSwitch") === "On"){
> Fan = { payload : flow.get("4")}; //BUG: if flow.get("4") is empty, then Fan.payload will be undefined 
> // Below is a safer way of getting flow vars (as it defaults to 0 if the flow var is empty)   
> Fan = { payload : flow.get("4") || 0};
> }else{
> Fan = { payload : 0}; //INFO: this is unnecessary if you initialise Fan in the first line
> }
> if (global.get("PSwittch") === "On"){
> Pump = { payload: flow.get("1") };//BUG
> }else{
> Pump = { payload : 0};
> }
> if (global.get("PHSwitch") === "On"){
> PowerH = { payload: flow.get("2") };//BUG
> }else{
> PowerH = { payload : 0};
> }
> if (global.get("APSwitch") === "On"){
> APump = { payload: flow.get("3") };//BUG
> }else{
> APump = { payload : 0};
> }
> 
> //return [Fan, Pump, PowerH, APump]; //INFO: unused
> msg.payload = (T);//BUG: T is Nan (you didnt update T). Also, the ( brackets ) are not necessary.
> return msg;
> 
> ```

[Here is some proof of your bugs - please take a look](https://repl.it/repls/NumbBeigeToolbox)

**My version of your code...**  
So assuming you wish output the total value of the 4 flow variables as the `msg.payload` based on some globals being === "On", then this code should be more robust and easier to debug (Attach a debug node set for 'complete msg' to the output)

```auto
//attach a debug node set for 'complete msg' to the output of 
//this function to see exactly what happened inside here.

//store global vars in msg for later use & easy debugging
msg.FSwitch = global.get("FSwitch");
msg.PSwitch = global.get("PSwitch");
msg.PHSwitch = global.get("PHSwitch");
msg.APSwitch = global.get("APSwitch");

//store flow vars in msg for later use & easy debugging
msg.flowVar1 = flow.get("1") || 0; 
msg.flowVar2 = flow.get("2") || 0;
msg.flowVar3 = flow.get("3") || 0;
msg.flowVar4 = flow.get("4") || 0;

msg.payload = 0; //Reset payload to zero before adding values   

if (msg.FSwitch === "On"){
    msg.payload += msg.flowVar4;
}
if (msg.PSwitch === "On"){
    msg.payload += msg.flowVar1;
}
if (msg.PHSwitch === "On"){
    msg.payload += msg.flowVar2;
}
if (msg.APSwitch === "On"){
    msg.payload += msg.flowVar3;
}

//Only once the msg.payload has been updated do we return it
return msg;

```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)\
**Post date:** [7 April 2019 20:34 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/36 "2019-04-07T20:34:48Z")

</div>

Thanks @Steve-Mcl for enlightening me with a informative post, that in my eyes is what a forum user should be doing to help another.  
Please advise where I should be reading for information on js as everything I have found so far carries alot of unwanted bloat ect, and is not even relative to node red.

---

<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:** [7 April 2019 20:39 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/37 "2019-04-07T20:39:24Z")

</div>

@rburton, you are right, my approach was bad, I do apologize

---

<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:** [7 April 2019 21:58 UTC](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513/38 "2019-04-07T21:58:57Z")

</div>

> [@rburton](#):
>
> Please advise where I should be reading for information on js as everything I have found so far carries alot of unwanted bloat ect.

I had a quick look and it seems many tutorials for js are heavily interlocked with html - but not what you're after.

I did spot [this](https://learn.freecodecamp.org/) though. It makes you do tests and it's all browser based.

I suggest you jump straight to the section "Javascript Algorithms And Data Structures Certification \> Basic JavaScript"

> [@rburton](#):
>
> and is not even relative to node red.

It's all relevant. Knowledge is power.

[Previous page](https://discourse.nodered.org/t/what-am-i-doing-wrong/9513.md?page=1)
