# How to pass msg value to switch topic field

**URL:** <https://discourse.nodered.org/t/how-to-pass-msg-value-to-switch-topic-field/43061>\
**Category:** Dashboard\
**Created:** [23 March 2021 08:00 UTC](https://discourse.nodered.org/t/how-to-pass-msg-value-to-switch-topic-field/43061 "2021-03-23T08:00:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddk](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@ddk](https://discourse.nodered.org/u/ddk)\
**Post date:** [23 March 2021 08:00 UTC](https://discourse.nodered.org/t/how-to-pass-msg-value-to-switch-topic-field/43061/1 "2021-03-23T08:00:04Z")

</div>

Hi

I want to set Topic filed with special expression:` {{{msg.prex}}}_test` in the switch node, but it not work.  
How can I fix it?

Thanks!!!

```auto
[{"id":"992e19b1.96722","type":"ui_switch","z":"7c707aa7.c06c4c","name":"loop","label":"loop","tooltip":"","group":"3fe9dd19.60393a","order":17,"width":3,"height":1,"passthru":true,"decouple":"false","topic":"{{{msg.prex}}}_test","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":490,"y":320,"wires":[["d02d8b77.f0b2f"]]},{"id":"8bfd9b7c.e99098","type":"inject","z":"7c707aa7.c06c4c","name":"","props":[{"p":"prex","v":"prex","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":320,"wires":[["992e19b1.96722"]]},{"id":"d02d8b77.f0b2f","type":"debug","z":"7c707aa7.c06c4c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":320,"wires":[]},{"id":"3fe9dd19.60393a","type":"ui_group","name":"rule1","tab":"4679b587.ba0d2c","order":2,"disp":true,"width":7,"collapse":true},{"id":"4679b587.ba0d2c","type":"ui_tab","name":"场景一","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 March 2021 08:35 UTC](https://discourse.nodered.org/t/how-to-pass-msg-value-to-switch-topic-field/43061/2 "2021-03-23T08:35:47Z")

</div>

Hi, in order to make code more readable and importable it is important to surround your code with three backticks  
`````  
`like this`  
`````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 March 2021 09:16 UTC](https://discourse.nodered.org/t/how-to-pass-msg-value-to-switch-topic-field/43061/3 "2021-03-23T09:16:25Z")

</div>

> [@ddk](#):
>
> I want to set Topic filed with special expression: ` {{{msg.prex}}}_test` in the switch node, but it not work.  
> How can I fix it?

It doesnt work like that. Use JSONata or a template node or a function node instead

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/32eca4d3ef72e076878fca59156f4e3fd2cb7391.png)

in a function...

```auto
msg.topic = msg.prex + "_test"
return msg;

```

as JSONata  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/89bfff723a12865d8b966f7488fb61ea3865e2c2.png)

in a template  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d319733e5ddf6962caea29595956755d4dcdafe9.png)

Demo flow...

```auto
[{"id":"92707120.1714","type":"change","z":"336fad55.d74c02","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"prex & \"_test\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":598,"y":416,"wires":[["1c5263f1.b1721c"]]},{"id":"c7fdea45.89c018","type":"template","z":"336fad55.d74c02","name":"","field":"topic","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{{prex}}}_test","output":"str","x":588,"y":464,"wires":[["6c7353f4.8f266c"]]},{"id":"5b5aa9d3.ba3608","type":"inject","z":"336fad55.d74c02","name":"msg.prex = \"hello\"","props":[{"p":"prex","v":"hello","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":378,"y":368,"wires":[["c7fdea45.89c018","92707120.1714","ef35e9ea.91a7b8"]]},{"id":"1c5263f1.b1721c","type":"debug","z":"336fad55.d74c02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"topic","targetType":"msg","statusVal":"topic","statusType":"auto","x":748,"y":416,"wires":[]},{"id":"6c7353f4.8f266c","type":"debug","z":"336fad55.d74c02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"topic","targetType":"msg","statusVal":"topic","statusType":"auto","x":750,"y":464,"wires":[]},{"id":"ef35e9ea.91a7b8","type":"function","z":"336fad55.d74c02","name":"","func":"msg.topic = msg.prex + \"_test\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":588,"y":368,"wires":[["30f0f154.562aee"]]},{"id":"30f0f154.562aee","type":"debug","z":"336fad55.d74c02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"topic","targetType":"msg","statusVal":"topic","statusType":"auto","x":748,"y":368,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![ddk](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@ddk](https://discourse.nodered.org/u/ddk)\
**Post date:** [24 March 2021 01:16 UTC](https://discourse.nodered.org/t/how-to-pass-msg-value-to-switch-topic-field/43061/4 "2021-03-24T01:16:22Z")

</div>

Thanks for your reply, I will try.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 April 2021 01:17 UTC](https://discourse.nodered.org/t/how-to-pass-msg-value-to-switch-topic-field/43061/5 "2021-04-07T01:17:21Z")

</div>

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