IF Else statement not working

I have an inject node that I use to send is various polaritysum values, some odd, some even.
The goal here is to test if the polaritysum is odd or even. I can see that the value of OddEven is either 1 or 0 as I expected by setting the msg.OddEven and looking at it in debug.

msg.payload is not being set for either odd or even values, and msg.topic is always thumbs up. msg.result is what I want but msg.payload is the same thing for some reason.

Any ideas??

var polaritysum = msg.polaritysum;
var OddEven = 0;
OddEven = polaritysum % 2;
msg.Oddeven = OddEven;
if (OddEven === 1) {
    msg.payload = true;
    msg.topic = "<i class=\"fa fa-thumbs-up\" aria-hidden=\"true\"></i>";
 } else {
    msg.payload = false;
    msg.topic = "<i class=\"fa fa-thumbs-down\" aria-hidden=\"true\"></i>";
    }; 
msg.result = "The polarity value is: " + polaritysum;
return msg;

'''
++++Debug results +++++++
{"_msgid":"ec92c6fb4e8a9277","polaritysum":9,"Oddeven":1,"payload":"The polarity value is: 9","topic":"<i class="fa fa-thumbs-up" aria-hidden="true">","result":"The polarity value is: 9"}

What does the inject node look like ?

Please select the inject node, function node and debug node and Export them and paste here.

If I inject msg.polaritysum (either odd or even) works for me (works if msg.polaritysum is a number or a string i.e. 9 or '9').
There must be something else going on. As others have asked, what are the outputs of both inject & function nodes.

[
    {
        "id": "875ee539e6ae511b",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "a47414adcb65bdbc",
        "type": "inject",
        "z": "875ee539e6ae511b",
        "name": "Left Light Source on Fiber 2 - \"B\" (Tranceiver B)",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "starting_fiber",
                "v": "2",
                "vt": "num"
            },
            {
                "p": "fiber",
                "v": "2",
                "vt": "num"
            },
            {
                "p": "polaritysum",
                "v": "0",
                "vt": "num"
            },
            {
                "p": "connector_type",
                "v": "LC",
                "vt": "str"
            },
            {
                "p": "gender",
                "v": "female",
                "vt": "str"
            },
            {
                "p": "gender_mismatch",
                "v": "false",
                "vt": "bool"
            },
            {
                "p": "connector_mismatch",
                "v": "false",
                "vt": "bool"
            },
            {
                "p": "port",
                "v": "1",
                "vt": "num"
            },
            {
                "p": "node_count",
                "v": "0",
                "vt": "num"
            },
            {
                "p": "link",
                "v": "{\t\"mynode0\": \"Tx\"    \t}\t",
                "vt": "jsonata"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Light Source Triggered",
        "payloadType": "str",
        "x": 660,
        "y": 340,
        "wires": [
            [
                "b4989f010eda6017"
            ]
        ]
    },
    {
        "id": "7f6d80db7286a0d4",
        "type": "function",
        "z": "875ee539e6ae511b",
        "name": "Is ODD or EVEN?",
        "func": "var polaritysum = msg.polaritysum;\nvar OddEven = 0;\nOddEven = polaritysum % 2;\nmsg.Oddeven = OddEven;\n// Odd is any non zero number, typically '1', so payload is true when odd\nif (OddEven === 1) {\n    msg.payload = true;\n    msg.topic = \"<i class=\\\"fa fa-thumbs-up\\\" aria-hidden=\\\"true\\\"></i>\";\n } else {\n    msg.payload = false;\n    msg.topic = \"<i class=\\\"fa fa-thumbs-down\\\" aria-hidden=\\\"true\\\"></i>\";\n    }; \nmsg.result = \"The polarity value is: \" + polaritysum;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1190,
        "y": 340,
        "wires": [
            [
                "45e7309e02989a55"
            ]
        ]
    },
    {
        "id": "b4989f010eda6017",
        "type": "function",
        "z": "875ee539e6ae511b",
        "name": "Patch Cord - AB",
        "func": "//This is a simulated patch cord.\n// The following variables are porperties of the patch cord\n// psum is short for polarity sum and refers to values from polarity deck\n// ctype is connector type property.  can be LC or MPO\n//lgender and rgender stand for gender on left and right side of node\nvar myleftgender = \"male\";\nvar myrightgender = \"male\";\nvar myleftctype = \"LC\";\nvar mynode_type = \"AB PatchCord\";\nvar fiberID = msg.fiber;\nvar psum = msg.polaritysum;\nvar node_counter = msg.node_count;\nvar node_logger = msg.node_log;\nvar mylocalnodes = msg.mynodes;\nvar ctype = msg.connector_type;\nvar gender = msg.gender;\n//\n//Polarity of this node is 1 so there is a +1 below\npsum = psum +1;\nmsg.polaritysum = psum;\nnode_counter = node_counter +1;\nmsg.node_count = node_counter;\n// Define this node as next key value pair in link object\nvar mynodevalue = \"mynode\" + node_counter;\nmsg.link[mynodevalue] = mynode_type;\n//Connector check...be sure ctype is set with connector_type\nif (myleftctype != ctype) {msg.connector_mismatch = true};\n//Gender check\nif (myleftgender ==gender) {msg.gender_mismatch = true};\nmsg.gender =myrightgender;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 960,
        "y": 340,
        "wires": [
            [
                "7f6d80db7286a0d4"
            ]
        ]
    },
    {
        "id": "45e7309e02989a55",
        "type": "debug",
        "z": "875ee539e6ae511b",
        "name": "Debug msg object",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1450,
        "y": 340,
        "wires": []
    },
    {
        "id": "83af50fcd0c70ac1",
        "type": "function",
        "z": "875ee539e6ae511b",
        "name": "Patch Cord - AB",
        "func": "//This is a simulated patch cord.\n// The following variables are porperties of the patch cord\n// psum is short for polarity sum and refers to values from polarity deck\n// ctype is connector type property.  can be LC or MPO\n//lgender and rgender stand for gender on left and right side of node\nvar myleftgender = \"male\";\nvar myrightgender = \"male\";\nvar myleftctype = \"LC\";\nvar mynode_type = \"AB PatchCord\";\nvar fiberID = msg.fiber;\nvar psum = msg.polaritysum;\nvar node_counter = msg.node_count;\nvar node_logger = msg.node_log;\nvar mylocalnodes = msg.mynodes;\nvar ctype = msg.connector_type;\nvar gender = msg.gender;\n//\n//Polarity of this node is 1 so there is a +1 below\npsum = psum +1;\nmsg.polaritysum = psum;\nnode_counter = node_counter +1;\nmsg.node_count = node_counter;\n// Define this node as next key value pair in link object\nvar mynodevalue = \"mynode\" + node_counter;\nmsg.link[mynodevalue] = mynode_type;\n//Connector check...be sure ctype is set with connector_type\nif (myleftctype != ctype) {msg.connector_mismatch = true};\n//Gender check\nif (myleftgender ==gender) {msg.gender_mismatch = true};\nmsg.gender =myrightgender;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 960,
        "y": 440,
        "wires": [
            [
                "624aab05a5468497"
            ]
        ]
    },
    {
        "id": "624aab05a5468497",
        "type": "function",
        "z": "875ee539e6ae511b",
        "name": "Patch Cord - AB",
        "func": "//This is a simulated patch cord.\n// The following variables are porperties of the patch cord\n// psum is short for polarity sum and refers to values from polarity deck\n// ctype is connector type property.  can be LC or MPO\n//lgender and rgender stand for gender on left and right side of node\nvar myleftgender = \"male\";\nvar myrightgender = \"male\";\nvar myleftctype = \"LC\";\nvar mynode_type = \"AB PatchCord\";\nvar fiberID = msg.fiber;\nvar psum = msg.polaritysum;\nvar node_counter = msg.node_count;\nvar node_logger = msg.node_log;\nvar mylocalnodes = msg.mynodes;\nvar ctype = msg.connector_type;\nvar gender = msg.gender;\n//\n//Polarity of this node is 1 so there is a +1 below\npsum = psum +1;\nmsg.polaritysum = psum;\nnode_counter = node_counter +1;\nmsg.node_count = node_counter;\n// Define this node as next key value pair in link object\nvar mynodevalue = \"mynode\" + node_counter;\nmsg.link[mynodevalue] = mynode_type;\n//Connector check...be sure ctype is set with connector_type\nif (myleftctype != ctype) {msg.connector_mismatch = true};\n//Gender check\nif (myleftgender ==gender) {msg.gender_mismatch = true};\nmsg.gender =myrightgender;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1160,
        "y": 440,
        "wires": [
            [
                "4c52b2729f3ec427"
            ]
        ]
    },
    {
        "id": "4c52b2729f3ec427",
        "type": "function",
        "z": "875ee539e6ae511b",
        "name": "Is ODD or EVEN?",
        "func": "var polaritysum = msg.polaritysum;\nvar OddEven = 0;\nOddEven = polaritysum % 2;\n//Adding to msg for debug purposes only\nmsg.Oddeven = OddEven;\n// Odd is any non zero number, typically '1', so payload is true when odd\nif (OddEven === 1) {\n    msg.payload = true;\n    msg.topic = \"<i class=\\\"fa fa-thumbs-up\\\" aria-hidden=\\\"true\\\"></i>\";\n } else {\n    msg.payload = false;\n    msg.topic = \"<i class=\\\"fa fa-thumbs-down\\\" aria-hidden=\\\"true\\\"></i>\";\n    }; \nmsg.result = \"The polarity value is: \" + polaritysum;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1370,
        "y": 440,
        "wires": [
            [
                "d8741be886094122"
            ]
        ]
    },
    {
        "id": "d8741be886094122",
        "type": "debug",
        "z": "875ee539e6ae511b",
        "name": "Debug msg object",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1590,
        "y": 440,
        "wires": []
    },
    {
        "id": "74783821fc8a4206",
        "type": "inject",
        "z": "875ee539e6ae511b",
        "name": "Left Light Source on Fiber 2 - \"B\" (Tranceiver B)",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "starting_fiber",
                "v": "2",
                "vt": "num"
            },
            {
                "p": "fiber",
                "v": "2",
                "vt": "num"
            },
            {
                "p": "polaritysum",
                "v": "0",
                "vt": "num"
            },
            {
                "p": "connector_type",
                "v": "LC",
                "vt": "str"
            },
            {
                "p": "gender",
                "v": "female",
                "vt": "str"
            },
            {
                "p": "gender_mismatch",
                "v": "false",
                "vt": "bool"
            },
            {
                "p": "connector_mismatch",
                "v": "false",
                "vt": "bool"
            },
            {
                "p": "port",
                "v": "1",
                "vt": "num"
            },
            {
                "p": "node_count",
                "v": "0",
                "vt": "num"
            },
            {
                "p": "link",
                "v": "{\t\"mynode0\": \"Tx\"    \t}\t",
                "vt": "jsonata"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "Light Source Triggered",
        "payloadType": "str",
        "x": 640,
        "y": 440,
        "wires": [
            [
                "83af50fcd0c70ac1"
            ]
        ]
    },
    {
        "id": "c07e38c6ce5f1598",
        "type": "comment",
        "z": "875ee539e6ae511b",
        "name": "Odd Use Case Test",
        "info": "",
        "x": 630,
        "y": 280,
        "wires": []
    },
    {
        "id": "4a2df5867fba80df",
        "type": "comment",
        "z": "875ee539e6ae511b",
        "name": "Even Use Case Test",
        "info": "",
        "x": 670,
        "y": 500,
        "wires": []
    }
]

Hi Colin. When I moved this to a new flow page for export and tested my uses cases there, it work. Something else in the app is not working. Let me dig a bit more since the code above is "working" as I would expect.

Hi everyone who replied to me. Thanks for the feedback! It worked when I copied this to a fresh flow and tried again. I have a link in/out node and some other nodes that must be impacting this code. I will dig a bit more to see if I can identify what is going on. I am new to JS and it took me a while to get the syntax right for this node. I was getting very frustrated that it looked like it should work but was not.

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