# Function with multiple outputs

**URL:** <https://discourse.nodered.org/t/function-with-multiple-outputs/57897>\
**Category:** General\
**Created:** [7 February 2022 05:23 UTC](https://discourse.nodered.org/t/function-with-multiple-outputs/57897 "2022-02-07T05:23:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [7 February 2022 05:23 UTC](https://discourse.nodered.org/t/function-with-multiple-outputs/57897/1 "2022-02-07T05:23:13Z")

</div>

Hi,  
I use often function with multiple outputs;  
If I need to send payload = "ON" (only one value) I write this function ....

```auto
var msg1 = {payload:"ON", topic:"light"}

```

but when I need to send payload = {"state":"ON"}

how I can write the msg1 ?

```auto
var msg1 = { ??? , topic:"light"}

```

---

<div class="post-metadata">

**Author:** ![trimmjens](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@trimmjens](https://discourse.nodered.org/u/trimmjens)\
**Post date:** [7 February 2022 06:08 UTC](https://discourse.nodered.org/t/function-with-multiple-outputs/57897/2 "2022-02-07T06:08:57Z")

</div>

Maybe like this?

```auto
var msg1 = {payload: msg.payload.state, topic:"light"}

```

---

<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:** [7 February 2022 07:28 UTC](https://discourse.nodered.org/t/function-with-multiple-outputs/57897/4 "2022-02-07T07:28:24Z")

</div>

`var msg1 = { payload: {state:“ON“} , topic:"light"};`

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [7 February 2022 07:39 UTC](https://discourse.nodered.org/t/function-with-multiple-outputs/57897/5 "2022-02-07T07:39:59Z")

</div>

If you need to return a message like that, payload should be an object and not a string or a number.

You can either declare a variable and define it as an object, then assign payload that variable (option 1), or just type it in the payload (option 2).

Option 1 (using a temporary variable, then assigning it to msg.payload):

```auto
var output = {};

// some code here?

output.state = "ON";

// some more code here?

msg.topic = "light";
msg.payload = output;
return msg;

```

Option 2 (working directly on the msg):

```auto
// some code first?

msg = { topic: "light", payload: {state: "ON"}};
return msg;

```

If you want to use the payload directly, you can also do it like this, note that I clear the msg payload first to make sure there are no variables other than the one you need:

```auto
// some code first?

msg.topic = "light";
msg.payload = {}; // this will clean the existing payload
msg.payload.state = "ON"; // adds the property and assigns it the value

return msg;

```

Using either option is a matter of personal preference. I tend to work on temporary variables, do all the operations there, and assign it to msg in the end.  
It wouldn't be the first time I unadvertently modify or overwrite the payload, clearing the previous value, then try to read another value contained in it and does not work, or I get a wrong value. Working as in option 1 prevents that. But if the function is simple enough, where you only have to read a handful of values, it shouldn't be a problem.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [7 February 2022 07:51 UTC](https://discourse.nodered.org/t/function-with-multiple-outputs/57897/6 "2022-02-07T07:51:53Z")

</div>

```auto
msg = { topic: "light", payload: {state: "ON"}};

```

Ok, this works for me.

Thank you very much for help 👍

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [7 February 2022 10:45 UTC](https://discourse.nodered.org/t/function-with-multiple-outputs/57897/7 "2022-02-07T10:45:38Z")

</div>

Thank you for explanation ....

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [7 February 2022 10:57 UTC](https://discourse.nodered.org/t/function-with-multiple-outputs/57897/8 "2022-02-07T10:57:43Z")

</div>

You're welcome 🙂

---

<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:** [21 February 2022 10:57 UTC](https://discourse.nodered.org/t/function-with-multiple-outputs/57897/9 "2022-02-21T10:57:51Z")

</div>

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