Switch Node if key exists

3,647 views
Skip to first unread message

Glenn

unread,
May 20, 2017, 11:51:32 PM5/20/17
to Node-RED
 Hopefully this is a simple question.

I am using a switch node to look for keys next and last

So it would switch on next.name and another for last.name
The problem is if the key next doesn't exist the switch node fails as it can't do next.name

Does this have to be done in a function?


steve rickus

unread,
May 21, 2017, 9:53:09 AM5/21/17
to Node-RED
Interesting question -- I had to try it out just to make sure I understood what you were asking, since I would not have expected this behavior...

It's really a simple test: check that the msg.payload.next.name field exists, and if so branch to port #1 (for example). Otherwise, branch to another port. This works great but only if the msg.payload.next object exists. In your case, when only msg.payload.last exists, the switch throws the exception "TypeError: Cannot read property 'name' of undefined"

I can understand why javascript would not be able to lookup a field on a non-existent object, but to have to have two switches in series to first check for the existence of the object, and then branch on it's value is quite inconvenient. As you mentioned, the more concise way would be to use a function, and add the check for the object before checking its value. Or, if you have control over the incoming msg format, remove one level of nesting and put the nextName and lastName directly on msg.payload.

However, I also checked the latest version in git and am happy to say that there will be a way to do in a switch node in 0.17 -- the "Property" to be check now has the option to be a JSONata expression. By using the expression option, I can safely check the result of evaluating payload.next.name (without the msg. prefix) and if that is not null, I know it exists. More clever expressions could also be used to return any one of a list of values (numbers, strings, whatever) that can be checked to route the msg to many ports, almost like a switch statement. Very nice...
--
Steve

Glenn

unread,
May 21, 2017, 6:02:04 PM5/21/17
to Node-RED
Ooh. Could be good. I don't like multiple switches

I end up doing a function which is messy

Eg if (!next) {}

Dave C-J

unread,
May 21, 2017, 7:13:21 PM5/21/17
to node...@googlegroups.com
Can you use the "not null" comparison on msg.payload.next ?

Glenn

unread,
May 21, 2017, 8:23:09 PM5/21/17
to Node-RED
I want to know if not null and contains a specific value can you do that with a switch

herrie jr

unread,
Jun 15, 2017, 7:03:56 AM6/15/17
to Node-RED
Hello all,

today I was stumbling upon this same problem. I managed to solve this with a switch and Jsonata.

In the Property field of a switch node, you select "Jsonata" and the Jsonata-expression is $string(msg.payload)

Then you can check if the string contains a certain value. This works for me.



Op maandag 22 mei 2017 02:23:09 UTC+2 schreef Glenn:

Glenn

unread,
Jun 15, 2017, 8:34:57 PM6/15/17
to Node-RED
Thanks. Can you post an example flow it would be quite helpful.

herrie jr

unread,
Jun 20, 2017, 11:11:07 AM6/20/17
to Node-RED
Here's an example:
- http://localhost:1880/request gets you to output 3
- http://localhost:1880/request?key1=x gets you to output 1
- http://localhost:1880/request?key2=x gets you to output 2

Hope this is clear enough. Pretty straightforward, but it matches also if you do http://localhost:1880/request?property=key1

[{"id":"fe60d660.34adb8","type":"http in","z":"d2441641.e71a8","name":"request URL","url":"/request","method":"get","swaggerDoc":"","x":258,"y":128,"wires":[["ba0c270.3b9afd8"]]},{"id":"ba0c270.3b9afd8","type":"switch","z":"d2441641.e71a8","name":"Key-switch","property":"$string(msg.payload)","propertyType":"jsonata","rules":[{"t":"cont","v":"key1","vt":"str"},{"t":"cont","v":"key2","vt":"str"},{"t":"else"}],"checkall":"false","outputs":3,"x":469,"y":335,"wires":[[],[],[]]}]

Op vrijdag 16 juni 2017 02:34:57 UTC+2 schreef Glenn:
Reply all
Reply to author
Forward
0 new messages