# Node function with 2 msg

**URL:** <https://discourse.nodered.org/t/node-function-with-2-msg/21605>\
**Category:** General\
**Created:** [12 February 2020 15:53 UTC](https://discourse.nodered.org/t/node-function-with-2-msg/21605 "2020-02-12T15:53:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pvklink](https://avatars.discourse-cdn.com/v4/letter/p/46a35a/32.png) [@pvklink](https://discourse.nodered.org/u/pvklink)\
**Post date:** [12 February 2020 15:53 UTC](https://discourse.nodered.org/t/node-function-with-2-msg/21605/1 "2020-02-12T15:53:33Z")

</div>

I am playing around with node-red and want to learn more...

Problem i cant solve is how to create two msg(msg1 and msg2) in a function  
This must be the output

//input msg for function  
// test0: "test0"

//msg1  
// test0: "test0"  
// test1: "test0"

// msg2  
// test0: "test0"  
// test2: "test2"

I can create twe two msg within the function, but msg2 only contains var test and not the old input content .

```auto
test = {"test2":"test2"} // new extra var for msg2. But how do i add old msg ?

// add var to current msg
msg1.test1="test1"

msg2=test; // how do i also add content of msg1 (current msg)
return[msg1,msg2]

```

---

<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:** [12 February 2020 15:59 UTC](https://discourse.nodered.org/t/node-function-with-2-msg/21605/2 "2020-02-12T15:59:28Z")

</div>

There are lots of ways of doing this - they are all just JavaScript objects in the end.  
Something like:

```auto
var msg1 = {
   test0: msg.test0,
   test1: "test0"
}

var msg2 = {
   test0: msg.test0,
   test2: "test2"
}

return [msg1,msg2]
```

---

<div class="post-metadata">

**Author:** ![pvklink](https://avatars.discourse-cdn.com/v4/letter/p/46a35a/32.png) [@pvklink](https://discourse.nodered.org/u/pvklink)\
**Post date:** [12 February 2020 16:48 UTC](https://discourse.nodered.org/t/node-function-with-2-msg/21605/3 "2020-02-12T16:48:52Z")

</div>

That part was the part that was working...

But what if msg2 has to contain (test2: "" test2) like you did AND ALL the content of the input message of the function.

something like msg2=msg + {test2: "test2"}

---

<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:** [12 February 2020 17:18 UTC](https://discourse.nodered.org/t/node-function-with-2-msg/21605/4 "2020-02-12T17:18:22Z")

</div>

```auto
var msg1 = RED.util.cloneMessage(msg);
var msg2 = RED.util.cloneMessage(msg);

// now update msg1 and msg2 with whatever extra properties you want
msg1.test = "red";
msg2.test = "blue";

return [msg1,msg2]

```

---

<div class="post-metadata">

**Author:** ![pvklink](https://avatars.discourse-cdn.com/v4/letter/p/46a35a/32.png) [@pvklink](https://discourse.nodered.org/u/pvklink)\
**Post date:** [12 February 2020 17:23 UTC](https://discourse.nodered.org/t/node-function-with-2-msg/21605/6 "2020-02-12T17:23:29Z")

</div>

> [@knolleary](#):
>
> ```auto
> var msg1 = RED.util.cloneMessage(msg);
> var msg2 = RED.util.cloneMessage(msg);
> 
> // now update msg1 and msg2 with whatever extra properties you want
> msg1.test = "red";
> msg2.test = "blue";
> 
> return [msg1,msg2]
> 
> ```

Thanks @knolleary, that works!

---

<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:** [26 February 2020 17:23 UTC](https://discourse.nodered.org/t/node-function-with-2-msg/21605/7 "2020-02-26T17:23:32Z")

</div>

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