# Can someone help me with this function node code?

**URL:** <https://discourse.nodered.org/t/can-someone-help-me-with-this-function-node-code/57362>\
**Category:** General\
**Created:** [27 January 2022 18:38 UTC](https://discourse.nodered.org/t/can-someone-help-me-with-this-function-node-code/57362 "2022-01-27T18:38:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![furrymayo](https://avatars.discourse-cdn.com/v4/letter/f/7cd45c/32.png) [@furrymayo](https://discourse.nodered.org/u/furrymayo)\
**Post date:** [27 January 2022 18:38 UTC](https://discourse.nodered.org/t/can-someone-help-me-with-this-function-node-code/57362/1 "2022-01-27T18:38:05Z")

</div>

Sorry if this isn't the right place but can anyone tell me why this code creates errors?

if (msg.payload === "on") msg.payload = {"service":"turn\_on"}  
if (msg.payload === "off") msg.payload = {"service":"turn\_off"}  
if (msg.payload === "play") msg.payload = {"service":"media\_play"}  
if (msg.payload === "pause") msg.payload = {"service":"media\_pause"}  
return msg

When I comment out two of the "if" lines then it runs perfectly. So, I assume I have a syntax issue?

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [27 January 2022 18:46 UTC](https://discourse.nodered.org/t/can-someone-help-me-with-this-function-node-code/57362/2 "2022-01-27T18:46:38Z")

</div>

I guess you are missing ";" - but as I am not a friend of function nodes - why are you not using a change node.

```auto
[{"id":"ea174b1fdc5e43af","type":"change","z":"cb05e14b.326a9","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"turn_on","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"turn_off","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"play","fromt":"str","to":"media_play","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"pause","fromt":"str","to":"media_pause","tot":"str"},{"t":"move","p":"payload","pt":"msg","to":"payload.service","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":2120,"wires":[["7323fea9071775a8"]]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [27 January 2022 18:49 UTC](https://discourse.nodered.org/t/can-someone-help-me-with-this-function-node-code/57362/3 "2022-01-27T18:49:40Z")

</div>

I ran you code but no issues. so would need more info like inputs to function

Your code would be better with if else statements and a null response if no match  
e.g.

```auto
if (msg.payload === "on") msg.payload = {"service":"turn_on"}
else if (msg.payload === "off") msg.payload = {"service":"turn_off"}
else if (msg.payload === "play") msg.payload = {"service":"media_play"}
else if (msg.payload === "pause") msg.payload = {"service":"media_pause"}
else msg = null
return msg

```

---

<div class="post-metadata">

**Author:** ![furrymayo](https://avatars.discourse-cdn.com/v4/letter/f/7cd45c/32.png) [@furrymayo](https://discourse.nodered.org/u/furrymayo)\
**Post date:** [27 January 2022 18:52 UTC](https://discourse.nodered.org/t/can-someone-help-me-with-this-function-node-code/57362/4 "2022-01-27T18:52:19Z")

</div>

Well, I just realized that the Play/Pause payload aren't working either alone as alexa says "device" doesn't support that.

---

<div class="post-metadata">

**Author:** ![furrymayo](https://avatars.discourse-cdn.com/v4/letter/f/7cd45c/32.png) [@furrymayo](https://discourse.nodered.org/u/furrymayo)\
**Post date:** [27 January 2022 18:53 UTC](https://discourse.nodered.org/t/can-someone-help-me-with-this-function-node-code/57362/5 "2022-01-27T18:53:09Z")

</div>

> [@mickym2](#):
>
> ```auto
> [{"id":"ea174b1fdc5e43af","type":"change","z":"cb05e14b.326a9","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"turn_on","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"turn_off","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"play","fromt":"str","to":"media_play","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"pause","fromt":"str","to":"media_pause","tot":"str"},{"t":"move","p":"payload","pt":"msg","to":"payload.service","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":2120,"wires":[["7323fea9071775a8"]]}]
> 
> ```

Mostly because I don't know how change nodes work. I am so new to all of this still

---

<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:** [28 March 2022 18:53 UTC](https://discourse.nodered.org/t/can-someone-help-me-with-this-function-node-code/57362/6 "2022-03-28T18:53:42Z")

</div>

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