# Read msg.topic?

**URL:** <https://discourse.nodered.org/t/read-msg-topic/8934>\
**Category:** General\
**Created:** [13 March 2019 15:46 UTC](https://discourse.nodered.org/t/read-msg-topic/8934 "2019-03-13T15:46:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![malcomxcia](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@malcomxcia](https://discourse.nodered.org/u/malcomxcia)\
**Post date:** [13 March 2019 15:46 UTC](https://discourse.nodered.org/t/read-msg-topic/8934/1 "2019-03-13T15:46:53Z")

</div>

Hello ! ! !

I will read the Tobic of an Message. This is in my Function Node

```auto
var topic = msg.topic;
return topic;

```

Alway comes in Debug Node

`Function tried to send a message of type string`

Where ist the Problem ?

Greets  
Malcom

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [13 March 2019 16:05 UTC](https://discourse.nodered.org/t/read-msg-topic/8934/2 "2019-03-13T16:05:50Z")

</div>

Hi @malcomxcia

A Function node must return a message object. It cannot return a simple String or Number type.

Your function is returning the value of `msg.topic` - which is not a message object, so this isn't a valid thing to do.

What do you want to do with `msg.topic`?

---

<div class="post-metadata">

**Author:** ![malcomxcia](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@malcomxcia](https://discourse.nodered.org/u/malcomxcia)\
**Post date:** [13 March 2019 16:25 UTC](https://discourse.nodered.org/t/read-msg-topic/8934/3 "2019-03-13T16:25:02Z")

</div>

Thank you for Answer 😉

I will testing to choose on an Dropdown where in the Value the topic of Lamps. I will write this Value to the Topic and in ..... next Step then use it to change with a Color Picker the color of the selected lamp.

`[{"id":"3846eb1f.abe4c4","type":"ui_dropdown","z":"7b7476ef.c490a8","name":"","label":"","tooltip":"","place":"Select option","group":"410b79ee.be3948","order":10,"width":0,"height":0,"passthru":true,"options":[{"label":"Lampe Neu","value":"cmnd/lampe_neu_1/color","type":"str"},{"label":"LED PC","value":"cmnd/led_stripe_pc/color","type":"str"}],"payload":"","topic":"test","x":120,"y":140,"wires":[["de830a35.ab6ac8","4c99cab5.9948d4"]]},{"id":"4c99cab5.9948d4","type":"function","z":"7b7476ef.c490a8","name":"","func":"var topic=msg.topic;\nreturn topic;","outputs":1,"noerr":0,"x":630,"y":400,"wires":[["9e3564eb.f6e0f8"]]},{"id":"410b79ee.be3948","type":"ui_group","z":"","name":"TESTPANEL","tab":"5486a51c.17010c","order":1,"disp":true,"width":"6","collapse":false},{"id":"5486a51c.17010c","type":"ui_tab","z":"","name":"TESTPANEL","icon":"dashboard","order":1,"disabled":false,"hidden":false}]`

The Tobic on the Dropdown for Testing is TEST and then it should give me the topic ?

Sorry im Newcomer an allways sorry for my English 😃

Malcom

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [13 March 2019 17:17 UTC](https://discourse.nodered.org/t/read-msg-topic/8934/4 "2019-03-13T17:17:01Z")

</div>

Step 1: Add some debug nodes to your flow so you can see the data. (set the to Output: Complete Message Object )

It will give you the topic variable within the function. But you cannot return a variable you have to return an object.

Step 2 : Read the documentation about working with messages.

```auto
[{"id":"310be48a.063bdc","type":"ui_dropdown","z":"e4ccc526.618838","name":"","label":"","place":"Select option","group":"673daf99.c8ffb8","order":10,"width":0,"height":0,"passthru":true,"options":[{"label":"Lampe Neu","value":"cmnd/lampe_neu_1/color","type":"str"},{"label":"LED PC","value":"cmnd/led_stripe_pc/color","type":"str"}],"payload":"","topic":"test","x":134,"y":114,"wires":[["8a8387d.d620af8","b3466b68.2e8d18"]]},{"id":"8a8387d.d620af8","type":"function","z":"e4ccc526.618838","name":"","func":"msg.payload = \"Output selected:\" + msg.topic;\nreturn msg;","outputs":1,"noerr":0,"x":418,"y":199,"wires":[["cdfa3a1.b9eaac8"]]},{"id":"cdfa3a1.b9eaac8","type":"debug","z":"e4ccc526.618838","name":"Function Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":640.5,"y":218,"wires":[]},{"id":"b3466b68.2e8d18","type":"debug","z":"e4ccc526.618838","name":"dropdown output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":408.5,"y":73,"wires":[]},{"id":"673daf99.c8ffb8","type":"ui_group","z":"","name":"TESTPANEL","tab":"f49a16d9.2d5f8","order":1,"disp":true,"width":"6","collapse":false},{"id":"f49a16d9.2d5f8","type":"ui_tab","z":"","name":"TESTPANEL","icon":"dashboard","order":1}]

```

---

<div class="post-metadata">

**Author:** ![malcomxcia](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@malcomxcia](https://discourse.nodered.org/u/malcomxcia)\
**Post date:** [15 March 2019 17:38 UTC](https://discourse.nodered.org/t/read-msg-topic/8934/5 "2019-03-15T17:38:09Z")

</div>

Thank you for your explanation.

Okay, now I get it. You can't assign an msg object to a normal variable. Should be logical and sorry 😉

Is it possible that I use the Topic from the Function Node in the Color Picker to further use it ?

For example, with a flow variable or what ever ?

Have a nice Weekend and Thank you again  
Malcom

 ![15-18-37-28](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/67c2613358ce94c1bb8f58fefcbe637563a42794.jpeg)
