Weakness in switch node

Hello,

While doing test on NR, I have been facing to this issue

13 Dec 07:35:45 - [error] [switch:c316c1362446b8d3] TypeError: Cannot read properties of null (reading 'hasOwnProperty')
at processMessage ([/home/mxxxxx/node_modules/@node-red/nodes/core/function/10-switch.js:444:39](mailto:/home/m11172/node_modules/@node-red/nodes/core/function/10-switch.js:444:39))
at processMessageQueue ([/home/mxxxxx/node_modules/@node-red/nodes/core/function/10-switch.js:504:13](mailto:/home/m11172/node_modules/@node-red/nodes/core/function/10-switch.js:504:13))
at SwitchNode._inputCallback ([/home/mxxxxx/node_modules/@node-red/nodes/core/function/10-switch.js:513:13](mailto:/home/m11172/node_modules/@node-red/nodes/core/function/10-switch.js:513:13))
at [/home/mxxxxx/node_modules/@node-red/runtime/lib/nodes/Node.js:210:26](mailto:/home/m11172/node_modules/@node-red/runtime/lib/nodes/Node.js:210:26)
at Object.trigger ([/home/mxxxxx/node_modules/@node-red/util/lib/hooks.js:166:13](mailto:/home/m11172/node_modules/@node-red/util/lib/hooks.js:166:13))
at Node._emitInput ([/home/mxxxxx/node_modules/@node-red/runtime/lib/nodes/Node.js:202:11](mailto:/home/m11172/node_modules/@node-red/runtime/lib/nodes/Node.js:202:11))
at Node.emit ([/home/mxxxxx/node_modules/@node-red/runtime/lib/nodes/Node.js:186:25](mailto:/home/m11172/node_modules/@node-red/runtime/lib/nodes/Node.js:186:25))
at Node.receive ([/home/mxxxxx/node_modules/@node-red/runtime/lib/nodes/Node.js:485:10](mailto:/home/m11172/node_modules/@node-red/runtime/lib/nodes/Node.js:485:10))
at Immediate._onImmediate ([/home/mxxxxx/node_modules/@node-red/runtime/lib/flows/Flow.js:831:52](mailto:/home/m11172/node_modules/@node-red/runtime/lib/flows/Flow.js:831:52))
at process.processImmediate (node:internal/timers:471:21)

after troubleshooting, this is related to having a msg with msg.parts= null
I will not said this is really a bug as issue is related to malformed packet.
after checking in the code,

  function processMessage(msg, checkParts, done) {
             var hasParts = msg.hasOwnProperty("parts") &&
                             msg.parts.hasOwnProperty("id") &&
                             msg.parts.hasOwnProperty("index");

I think it may be interesting to replace by new JS notation

 var hasParts = !!(msg?.parts?.id &&
                          msg?.parts?.index);

which in case of malformed msg, will not break the flow any more

AB

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