Should UI nodes create an empty msg.topic if it is left blank?

This question comes up as a result of another thread where a ui-button sent a msg to a ui-text node and the ui-text node had the label set to {{msg.topic}}. The problem was that the topic in the ui-button was left empty. This causes a problem in the ui-text node when it tries to evaluate the label since there is no msg.topic in the object.

The problem (as I see it) is because of the logic setting msg.topic.

ui-form has no input option so the code in ui-form.js (line 31) that sets msg.topic is:

                msg.topic = config.topic;

Which works fine. The other nodes with this issue all use this code to set msg.topic:

                msg.topic = config.topic || msg.topic;

However if config.topic is empty AND msg.topic is empty, then neither case is TRUE so msg.topic is not set. This can be corrected by changing that line to:

                msg.topic = config.topic || msg.topic || "";

and an empty msg.topic will be build in the outgoing msg.

The following are the nodes that need(?) this fix:
ui_button.js - line: 54
ui_colour_picker.js - line: 46
ui_date_picker.js - line: 41
ui_dropdown.js - line: 127
ui_numeric.js - line: 39
ui_slider.js - line: 36
ui_switch.js - line: 123
ui_text_input.js - line: 37

I will attempt a PR if given the go ahead :upside_down_face:

1 Like

so All the node are wrong ? - or maybe they are all correct... msgs don't need to have a topic property - and may not come from ui nodes. A better fix would be to handle the label properly. but if I try a ui_text node with label set to {{msg.topic}} and send a message with no topic it works fine... the label is empty but so what ... what am I missing ?

[{"id":"ae7dc5cf.2d5648","type":"ui_text","z":"3658255b.2041aa","group":"bcb7ffa4.a2c8e","order":2,"width":0,"height":0,"name":"","label":"{{msg.topic}}","format":"{{msg.payload}}","layout":"row-spread","x":530,"y":640,"wires":[]},{"id":"9275fb95.86a068","type":"inject","z":"3658255b.2041aa","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":640,"wires":[["9be8cdc0.f90f3"]]},{"id":"9be8cdc0.f90f3","type":"function","z":"3658255b.2041aa","name":"","func":"delete msg.topic;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":640,"wires":[["ae7dc5cf.2d5648","9f8316b7.a08348"]]},{"id":"bcb7ffa4.a2c8e","type":"ui_group","z":"","name":"Default","tab":"28e3359c.db4eaa","disp":true,"width":"6","collapse":false},{"id":"28e3359c.db4eaa","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Very strange, if you add a ui_button to your flow and press the button, you get the json error when you press the button

[{"id":"ae7dc5cf.2d5648","type":"ui_text","z":"3658255b.2041aa","group":"bcb7ffa4.a2c8e","order":2,"width":0,"height":0,"name":"","label":"{{msg.topic}}","format":"{{msg.payload}}","layout":"row-spread","x":530,"y":640,"wires":[]},{"id":"9275fb95.86a068","type":"inject","z":"3658255b.2041aa","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":640,"wires":[["9be8cdc0.f90f3"]]},{"id":"9be8cdc0.f90f3","type":"function","z":"3658255b.2041aa","name":"","func":"delete msg.topic;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":640,"wires":[["ae7dc5cf.2d5648","9f8316b7.a08348"]]},{"id":"bcb7ffa4.a2c8e","type":"ui_group","z":"","name":"Default","tab":"28e3359c.db4eaa","disp":true,"width":"6","collapse":false},{"id":"28e3359c.db4eaa","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

the only difference I see in the debug is the ui_button has a socket ID

@dceejay Dave, this is really crazy. If you send the button output to a change node and first add a blank topic then delete msg.topic, no error occurs.

[{"id":"5738e8f2.e335a8","type":"ui_button","z":"5e2c387f.8cd64","name":"Case 1 - gets json error","group":"7427e564.9f901c","order":1,"width":0,"height":0,"passthru":false,"label":"Case 1","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":190,"y":60,"wires":[["f1e5bb70.43789","ce21183a.16712"]]},{"id":"f1e5bb70.43789","type":"debug","z":"5e2c387f.8cd64","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":430,"y":100,"wires":[]},{"id":"ce21183a.16712","type":"ui_text","z":"5e2c387f.8cd64","group":"7427e564.9f901c","order":2,"width":0,"height":0,"name":"","label":"{{msg.topic}}","format":"{{msg.payload}}","layout":"row-spread","x":710,"y":60,"wires":[]},{"id":"421b3dc8.1f3514","type":"ui_button","z":"5e2c387f.8cd64","name":"Case 2 - NO json error","group":"7427e564.9f901c","order":1,"width":0,"height":0,"passthru":false,"label":"Case 2","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":180,"y":160,"wires":[["c13d6a6f.5650f","6fb284bf.88877c"]]},{"id":"c13d6a6f.5650f","type":"debug","z":"5e2c387f.8cd64","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":430,"y":220,"wires":[]},{"id":"24c4c69e.a6e182","type":"ui_text","z":"5e2c387f.8cd64","group":"7427e564.9f901c","order":2,"width":0,"height":0,"name":"","label":"{{msg.topic}}","format":"{{msg.payload}}","layout":"row-spread","x":710,"y":160,"wires":[]},{"id":"6fb284bf.88877c","type":"change","z":"5e2c387f.8cd64","name":"add then delete msg.topic","rules":[{"t":"set","p":"topic","pt":"msg","to":"","tot":"str"},{"t":"delete","p":"topic","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":160,"wires":[["24c4c69e.a6e182","d22aa13f.8091d"]]},{"id":"d22aa13f.8091d","type":"debug","z":"5e2c387f.8cd64","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":220,"wires":[]},{"id":"7427e564.9f901c","type":"ui_group","z":"","name":"Default","tab":"d668501e.c0f0b8","disp":true,"width":"6","collapse":false},{"id":"d668501e.c0f0b8","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

most odd - but yes - def more a bug on the label handling side than lack of topic side.

Ok - fixed on master. The label (and others) were not handling if the chosen property was present but undefined.

1 Like

@dceejay Dave, would it make good programming sense to still change the ui nodes so they don't create an undefined msg.topic?

Feel free to raise a pr

The PR has been submitted