# If Else Problem

**URL:** <https://discourse.nodered.org/t/if-else-problem/15709>\
**Category:** General\
**Created:** [20 September 2019 07:46 UTC](https://discourse.nodered.org/t/if-else-problem/15709 "2019-09-20T07:46:06Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![hikmet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hikmet/32/11983_2.png) [@hikmet](https://discourse.nodered.org/u/hikmet)\
**Post date:** [20 September 2019 07:46 UTC](https://discourse.nodered.org/t/if-else-problem/15709/1 "2019-09-20T07:46:06Z")

</div>

Hi,  
This problem can be very simple for you. But I couldn't.

var gelen = msg.payload;  
var son =flow.get('son');  
if (gelen == son) {msg.payload = " No Action "} else {msg.payload =" Write DB "}  
flow.set('son',gelen);  
msg.payload = "G:" + gelen + " S:" + son + " - " + msg.payload;  
return msg

The gelen and son value is the same or not. The result does not change.

Output Sample : 1  
G:1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 S:1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 - Write DB

Output Sample : 2  
G:1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 S:1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 - Write DB

My expectation was that there was "No Action" in example 1.

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [20 September 2019 08:18 UTC](https://discourse.nodered.org/t/if-else-problem/15709/2 "2019-09-20T08:18:23Z")

</div>

I think You are comparing two objects and not the content of the objects. Two objects are never the same.  
You can try  
`Object.toJSON(glen)==Object.toJSON(son)`

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [20 September 2019 08:24 UTC](https://discourse.nodered.org/t/if-else-problem/15709/3 "2019-09-20T08:24:29Z")

</div>

Since the `RED.util` helper is available in function nodes, too, you could use `RED.util.compareObjects(o1, o2)`.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 September 2019 08:53 UTC](https://discourse.nodered.org/t/if-else-problem/15709/5 "2019-09-20T08:53:03Z")

</div>

> [@cymplecy](#):
>
> ```auto
> msg.payload = "G:" + gelen + " S:" + son + " - " + msg.payload;
> 
> ```
> 
> msg.payload changed so previous value of "No Action" or "Write DB" is ignored

That line appends the old msg.payload to the new one, so it should still be there.

---

<div class="post-metadata">

**Author:** ![hikmet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hikmet/32/11983_2.png) [@hikmet](https://discourse.nodered.org/u/hikmet)\
**Post date:** [20 September 2019 09:11 UTC](https://discourse.nodered.org/t/if-else-problem/15709/7 "2019-09-20T09:11:40Z")

</div>

> [@Christian-Me](#):
>
> I think You are comparing two objects and not the content of the objects. Two objects are never the same.

No, this 2 content is same.

---

<div class="post-metadata">

**Author:** ![hikmet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hikmet/32/11983_2.png) [@hikmet](https://discourse.nodered.org/u/hikmet)\
**Post date:** [20 September 2019 09:12 UTC](https://discourse.nodered.org/t/if-else-problem/15709/8 "2019-09-20T09:12:42Z")

</div>

This code is error. "TypeError: Object.toJSON is not a function"

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [20 September 2019 09:15 UTC](https://discourse.nodered.org/t/if-else-problem/15709/9 "2019-09-20T09:15:32Z")

</div>

I never had an issue with `RED.util.compareObjects(o1, o2)`.  
What kind of objects are you trying to compare?

---

<div class="post-metadata">

**Author:** ![hikmet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hikmet/32/11983_2.png) [@hikmet](https://discourse.nodered.org/u/hikmet)\
**Post date:** [20 September 2019 09:17 UTC](https://discourse.nodered.org/t/if-else-problem/15709/10 "2019-09-20T09:17:39Z")

</div>

information returned from modbus.  
But I'm throwing both into variables.  
Both look the same when I upload it to msg.payload.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [20 September 2019 09:18 UTC](https://discourse.nodered.org/t/if-else-problem/15709/11 "2019-09-20T09:18:19Z")

</div>

I know I am not the best person to reply to structures of programs,  
but I would like to suggest a couple of things:

And you seem better at doing one line tricks than I am.

Your code as originally posted:

```auto
var gelen = msg.payload;
var son =flow.get('son');
if (gelen == son) {msg.payload = " No Action "} else {msg.payload =" Write DB "}
flow.set('son',gelen);
msg.payload = "G:" + gelen + " S:" + son + " - " + msg.payload;
return msg

```

Line 1: Why are you assigning a _name_ to an unknown thing?  
The `msg.payload` could/can be anything.

It would help with readability if you made it something like:

```auto
var a = msg.payload;
var son = flow.get('son');
if (a == son)

```

I also advocate breaking things down to lines if they aren't working.  
But maybe that is because I am not good at programming.

```auto
if (a == son)
{
   msg.payload = "No action";
}
else
{
   msg.payload = "Write DB";
}
flow.set('son',a);
msg.payload = "G:" + gelen + " S:" + son + " - " + msg.payload;
return msg

```

Then you can put `node.warn( )` in there and track what is going on a lot easier.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 September 2019 09:47 UTC](https://discourse.nodered.org/t/if-else-problem/15709/12 "2019-09-20T09:47:58Z")

</div>

It might look simple, but this complicates a lot of things.

> [@hikmet](#):
>
> `msg.payload = "G:" + gelen + " S:" + son + " - " + msg.payload;`

~~`gelen` = `msg.payload` as defined in the first line, and you append again as `msg.payload`.~~

You see i am even more confused.

I would keep it simple like @Trying_to_learn

```auto
var gelen = msg.payload;
var son =context.get('son');

if (gelen == son) {
    action = " No Action "
}else{ 
    action = " Write DB "
}

context.set('son',gelen);

result = "G:" + gelen + " S:" + son + " - " + action

return {payload:result}

```

---

<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:** [20 September 2019 10:25 UTC](https://discourse.nodered.org/t/if-else-problem/15709/13 "2019-09-20T10:25:57Z")

</div>

What is the data type of both son and gelen? If you add a debug to the node feeding the function node you will see the type of msg.payload and in the sidebar select the context tab (last one on the right looks like a disk icon) you can see the type of son.

---

<div class="post-metadata">

**Author:** ![hikmet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hikmet/32/11983_2.png) [@hikmet](https://discourse.nodered.org/u/hikmet)\
**Post date:** [20 September 2019 10:43 UTC](https://discourse.nodered.org/t/if-else-problem/15709/14 "2019-09-20T10:43:56Z")

</div>

2 variable is array[24]

---

<div class="post-metadata">

**Author:** ![hikmet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hikmet/32/11983_2.png) [@hikmet](https://discourse.nodered.org/u/hikmet)\
**Post date:** [20 September 2019 10:55 UTC](https://discourse.nodered.org/t/if-else-problem/15709/15 "2019-09-20T10:55:58Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/955fa6ccd41b805e5077f5f58256e7dbe8b78c0b.png)

---

<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:** [20 September 2019 10:59 UTC](https://discourse.nodered.org/t/if-else-problem/15709/16 "2019-09-20T10:59:10Z")

</div>

A simple function to compare two arrays

```auto
var b = [1, 2, 3, 5]; 
// comparing both arrays using stringify 
if(JSON.stringify(msg.payload)==JSON.stringify(b)) 
{ msg.payload = true
    
}else {
 msg.payload = false
}
return msg;

```

a flow that shows it working

`[{"id":"9bb3114.17ac2f","type":"inject","z":"a3fa6d78.e044a8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110.5,"y":364,"wires":[["e1c98e1b.0cdd3"]]},{"id":"e5562d95.e6d828","type":"function","z":"a3fa6d78.e044a8","name":"","func":"\nvar b = [1, 2, 3, 5]; \n// comparing both arrays using stringify \nif(JSON.stringify(msg.payload)==JSON.stringify(b)) \n{ msg.payload = true\n \n}else {\n msg.payload = false\n}\nreturn msg;","outputs":1,"noerr":0,"x":416.5,"y":431,"wires":[["c3c5d052.5ec17"]]},{"id":"c3c5d052.5ec17","type":"debug","z":"a3fa6d78.e044a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":600.5,"y":457,"wires":[]},{"id":"e1c98e1b.0cdd3","type":"function","z":"a3fa6d78.e044a8","name":"msg.payload = [1, 2, 3, 5]; ","func":"msg.payload = [1, 2, 3, 5]; \nreturn msg;","outputs":1,"noerr":0,"x":332.5,"y":366,"wires":[["e5562d95.e6d828"]]},{"id":"c7fb5d2e.667f48","type":"inject","z":"a3fa6d78.e044a8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":91,"y":523,"wires":[["39179bc4.3febec"]]},{"id":"39179bc4.3febec","type":"function","z":"a3fa6d78.e044a8","name":"msg.payload = [1, 2, 3, 5]; ","func":"msg.payload = [1, 2, 3, 4]; \nreturn msg;","outputs":1,"noerr":0,"x":313,"y":525,"wires":[["e5562d95.e6d828"]]}]`

---

<div class="post-metadata">

**Author:** ![hikmet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hikmet/32/11983_2.png) [@hikmet](https://discourse.nodered.org/u/hikmet)\
**Post date:** [20 September 2019 11:13 UTC](https://discourse.nodered.org/t/if-else-problem/15709/17 "2019-09-20T11:13:10Z")

</div>

> [@ukmoose](#):
>
> [{"id":"9bb3114.17ac2f","type":"inject","z":"a3fa6d78.e044a8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110.5,"y":364,"wires":[["e1c98e1b.0cdd3"]]},{"id":"e5562d95.e6d828","type":"function","z":"a3fa6d78.e044a8","name":"","func":"\nvar b = [1, 2, 3, 5]; \n// comparing both arrays using stringify \nif(JSON.stringify(msg.payload)==JSON.stringify(b)) \n{ msg.payload = true\n \n}else {\n msg.payload = false\n}\nreturn msg;","outputs":1,"noerr":0,"x":416.5,"y":431,"wires":[["c3c5d052.5ec17"]]},{"id":"c3c5d052.5ec17","type":"debug","z":"a3fa6d78.e044a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":600.5,"y":457,"wires":},{"id":"e1c98e1b.0cdd3","type":"function","z":"a3fa6d78.e044a8","name":"msg.payload = [1, 2, 3, 5]; ","func":"msg.payload = [1, 2, 3, 5]; \nreturn msg;","outputs":1,"noerr":0,"x":332.5,"y":366,"wires":[["e5562d95.e6d828"]]},{"id":"c7fb5d2e.667f48","type":"inject","z":"a3fa6d78.e044a8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":91,"y":523,"wires":[["39179bc4.3febec"]]},{"id":"39179bc4.3febec","type":"function","z":"a3fa6d78.e044a8","name":"msg.payload = [1, 2, 3, 5]; ","func":"msg.payload = [1, 2, 3, 4]; \nreturn msg;","outputs":1,"noerr":0,"x":313,"y":525,"wires":[["e5562d95.e6d828"]]}]

Thank you ukmoose, this solutions is true. 🙂
