Javascript switch in function node problem

Hi,

I was wondering if it was possible to compare values from different payloads in the same function. I keep getting an undefined error for my msg.payload[1]. Or if we can't, anybody would have a better idea?

Here is my flow:

[{"id":"fd617437.ba4bd8","type":"function","z":"14f8d12a.1216bf","name":"Switch","func":"host = msg.topic;\nbattery = msg.payload[0];\npower = msg.payload[1];\n\nswitch(host) {\n    case \"mic1\" :\n        if (battery === \"< REP 1 BATT_BARS 005 >\") {\n            msg.payload.battery = '<i class=\"fa fa-battery-full\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 004 >\") {\n            msg.payload.battery = '<i class=\"fa fa-battery-three-quarters\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 003 >\") {\n            msg.payload.battery = '<i class=\"fa fa-battery-three-quarters\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 002 >\") {\n            msg.payload.battery = '<i class=\"fa fa-battery-half\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 001 >\") {\n            msg.payload.battery = '<font color=\"yellow\"><i class=\"fa fa-battery-quarter\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 000 >\") {\n            msg.payload.battery = '<font color=\"red\"><i class=\"fa fa-battery-empty\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 255 >\") {\n            msg.payload.battery = '<font color=\"yellow\"><i class=\"fa fa-exclamation-triangle\"></i>';\n            return [msg, null];\n        } else if (power === \"< REP 1 TX_RF_PWR UNKN >\") {\n            msg.payload.battery = '<font color=\"red\"><i class=\"fa fa-times-circle\"></i>';\n            return [msg, null];\n        }\n    break;\n  default:\n    msg.payload = \"Unknown command\" ;\n    return [null, null, msg];\n    }\nreturn msg;","outputs":3,"noerr":0,"initialize":"","finalize":"","x":1670,"y":560,"wires":[["5a63a0d2.0192e","639ebaaf.06ff14"],[],["fa5eec2a.6b602"]]},{"id":"5a63a0d2.0192e","type":"debug","z":"14f8d12a.1216bf","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload.battery","targetType":"msg","statusVal":"payload","statusType":"auto","x":1920,"y":480,"wires":[]},{"id":"639ebaaf.06ff14","type":"debug","z":"14f8d12a.1216bf","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload.power","targetType":"msg","statusVal":"payload","statusType":"auto","x":1910,"y":540,"wires":[]},{"id":"fa5eec2a.6b602","type":"debug","z":"14f8d12a.1216bf","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1890,"y":600,"wires":[]},{"id":"3add34e0.bab05c","type":"inject","z":"14f8d12a.1216bf","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"mic1","payload":"< REP 1 BATT_BARS 005 >","payloadType":"str","x":1270,"y":540,"wires":[["afe300da.e3e17"]]},{"id":"ddecd044.debda","type":"inject","z":"14f8d12a.1216bf","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"0.5","topic":"mic1","payload":"< REP 1 TX_RF_PWR UNKN >","payloadType":"str","x":1260,"y":580,"wires":[["afe300da.e3e17"]]},{"id":"afe300da.e3e17","type":"join","z":"14f8d12a.1216bf","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"1","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1530,"y":560,"wires":[["fd617437.ba4bd8","4354a72f.531cd8"]]},{"id":"4354a72f.531cd8","type":"debug","z":"14f8d12a.1216bf","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1670,"y":620,"wires":[]}]

Yes, the problem you have is you are joining as an array, but the messages can come in any order, so your array could be reversed. Try joining as an object
e.g.

[{"id":"ddecd044.debda","type":"inject","z":"af0e674c.7dafb8","name":"","props":[{"p":"payload"},{"p":"id","v":"power","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"0.5","topic":"mic1","payload":"< REP 1 TX_RF_PWR UNKN >","payloadType":"str","x":210,"y":580,"wires":[["afe300da.e3e17"]]},{"id":"afe300da.e3e17","type":"join","z":"af0e674c.7dafb8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"id","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":440,"y":560,"wires":[["fd617437.ba4bd8","4354a72f.531cd8"]]},{"id":"3add34e0.bab05c","type":"inject","z":"af0e674c.7dafb8","name":"","props":[{"p":"payload"},{"p":"id","v":"battery","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"mic1","payload":"< REP 1 BATT_BARS 005 >","payloadType":"str","x":180,"y":540,"wires":[["afe300da.e3e17"]]},{"id":"fd617437.ba4bd8","type":"function","z":"af0e674c.7dafb8","name":"Switch","func":"host = msg.topic;\nbattery = msg.payload.battery;\npower = msg.payload.power;\n\nswitch(host) {\n    case \"mic1\" :\n        if (battery === \"< REP 1 BATT_BARS 005 >\") {\n            msg.payload.battery = '<i class=\"fa fa-battery-full\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 004 >\") {\n            msg.payload.battery = '<i class=\"fa fa-battery-three-quarters\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 003 >\") {\n            msg.payload.battery = '<i class=\"fa fa-battery-three-quarters\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 002 >\") {\n            msg.payload.battery = '<i class=\"fa fa-battery-half\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 001 >\") {\n            msg.payload.battery = '<font color=\"yellow\"><i class=\"fa fa-battery-quarter\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 000 >\") {\n            msg.payload.battery = '<font color=\"red\"><i class=\"fa fa-battery-empty\"></i>';\n            return [msg, null];\n        } else if (battery === \"< REP 1 BATT_BARS 255 >\") {\n            msg.payload.battery = '<font color=\"yellow\"><i class=\"fa fa-exclamation-triangle\"></i>';\n            return [msg, null];\n        } else if (power === \"< REP 1 TX_RF_PWR UNKN >\") {\n            msg.payload.battery = '<font color=\"red\"><i class=\"fa fa-times-circle\"></i>';\n            return [msg, null];\n        }\n    break;\n  default:\n    msg.payload = \"Unknown command\" ;\n    return [null, null, msg];\n    }\nreturn msg;","outputs":3,"noerr":0,"initialize":"","finalize":"","x":580,"y":560,"wires":[["5a63a0d2.0192e","639ebaaf.06ff14"],[],["fa5eec2a.6b602"]]},{"id":"4354a72f.531cd8","type":"debug","z":"af0e674c.7dafb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":580,"y":620,"wires":[]},{"id":"5a63a0d2.0192e","type":"debug","z":"af0e674c.7dafb8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload.battery","targetType":"msg","statusVal":"payload","statusType":"auto","x":830,"y":480,"wires":[]},{"id":"639ebaaf.06ff14","type":"debug","z":"af0e674c.7dafb8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload.power","targetType":"msg","statusVal":"payload","statusType":"auto","x":820,"y":540,"wires":[]},{"id":"fa5eec2a.6b602","type":"debug","z":"af0e674c.7dafb8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":800,"y":600,"wires":[]}]

That makes sense, I'll try that. Thanks!!

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