# Function: how to create a message with an array (beginner)

**URL:** <https://discourse.nodered.org/t/function-how-to-create-a-message-with-an-array-beginner/55356>\
**Category:** General\
**Created:** [17 December 2021 19:42 UTC](https://discourse.nodered.org/t/function-how-to-create-a-message-with-an-array-beginner/55356 "2021-12-17T19:42:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Speedriff](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Speedriff](https://discourse.nodered.org/u/Speedriff)\
**Post date:** [17 December 2021 19:42 UTC](https://discourse.nodered.org/t/function-how-to-create-a-message-with-an-array-beginner/55356/1 "2021-12-17T19:42:44Z")

</div>

Hi programmers,

I'm trying to programm a function, which should transform the message of a dashboard-colopicker to a message, which is readable from a HUEmagic node.

The colorpicker sends this message:

![colopicker](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/78e39599a7935ff243edf2110c3fcaee34881468.jpeg)

The HUEmagic group, which I want to control (I want to change the RGB values) sends this:

 ![HUE_GROUP](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/9150579834da4d97182f443a2c84f7d35e92de52.jpeg)

So I want to create a function, which takes the color-picker rgb values and makes a message with a payload.rgb.

My function doesn't work, here it is:

```auto
// Variablen

var colorR;
var colorG;
var colorB;

/// Programm

colorR = msg.payload.r; 
colorG = msg.payload.g;
colorB = msg.payload.b;

var msg_neu ={};
msg_neu.payload.rgb[0] = colorR;
msg_neu.payload.rgb[1] = colorG;
msg_neu.payload.rgb[2] = colorB;

return [msg_neu];

```

The debug node of the function says:

```auto
[TypeError: Cannot read property 'rgb' of undefined]

```

Hope I formatted everything nice and I hope you can help me. I think it's a format error, but don't know how to fix it..

Thanks for your advice!

Bye, Chris

---

<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:** [17 December 2021 19:53 UTC](https://discourse.nodered.org/t/function-how-to-create-a-message-with-an-array-beginner/55356/2 "2021-12-17T19:53:21Z")

</div>

You need to initialise `msg_neu.payload = {}` and `msg_neu.payload.rgb = []`

---

<div class="post-metadata">

**Author:** ![Speedriff](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Speedriff](https://discourse.nodered.org/u/Speedriff)\
**Post date:** [17 December 2021 20:01 UTC](https://discourse.nodered.org/t/function-how-to-create-a-message-with-an-array-beginner/55356/3 "2021-12-17T20:01:31Z")

</div>

Thanks! That worked. Really had troubles to find a nice tutorial to this but now it 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:** [31 December 2021 20:02 UTC](https://discourse.nodered.org/t/function-how-to-create-a-message-with-an-array-beginner/55356/4 "2021-12-31T20:02:27Z")

</div>

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