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

**URL:** <https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234>\
**Category:** Dashboard\
**Created:** [7 September 2019 13:35 UTC](https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234 "2019-09-07T13:35:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 September 2019 13:35 UTC](https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234/1 "2019-09-07T13:35:26Z")

</div>

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:

```auto
                msg.topic = config.topic;

```

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

```auto
                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:

```auto
                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 🙃

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [7 September 2019 14:35 UTC](https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234/2 "2019-09-07T14:35:47Z")

</div>

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 ?

```auto
[{"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}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 September 2019 14:52 UTC](https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234/3 "2019-09-07T14:52:03Z")

</div>

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

```auto
[{"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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 September 2019 16:02 UTC](https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234/4 "2019-09-07T16:02:44Z")

</div>

@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.

```auto
[{"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}]

```

 ![26%20PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d399227b823e3b81b4ffdbb5e93b547c3bd4c406.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [7 September 2019 17:12 UTC](https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234/5 "2019-09-07T17:12:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [7 September 2019 17:47 UTC](https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234/6 "2019-09-07T17:47:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 September 2019 22:17 UTC](https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234/7 "2019-09-07T22:17:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 September 2019 17:38 UTC](https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234/8 "2019-09-09T17:38:12Z")

</div>

Feel free to raise a pr

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 September 2019 11:51 UTC](https://discourse.nodered.org/t/should-ui-nodes-create-an-empty-msg-topic-if-it-is-left-blank/15234/9 "2019-09-10T11:51:50Z")

</div>

The PR has been submitted
