If Else Problem

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.

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)

1 Like

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

1 Like

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

No, this 2 content is same.

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

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

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

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:

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:

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.

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.

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

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

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}

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.

2 variable is array[24]

image

A simple function to compare two arrays

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"]]}]

Thank you ukmoose, this solutions is true. :slight_smile:

1 Like