# Trying to set a payload.name in a function node

**URL:** <https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645>\
**Category:** General\
**Created:** [3 December 2022 16:22 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645 "2022-12-03T16:22:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [3 December 2022 16:22 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/1 "2022-12-03T16:22:05Z")

</div>

I am setting values in my function:

```auto
let sync = context.get('sync') || {};
const nodeName = msg.nodeName;
sync[nodeName] = msg.payload.value;
const sync_array = Object.values(sync);

if (sync_array.length >= 3) {
    msg = { payload: (sync_array.reduce((acc, num) => acc + num, 0) + " Watts"), color: "lime", strongcolor: "deepSkyBlue", nodeName: "Plinth Lights" 
}
    sync = undefined;
}else{
    msg = null;
}

context.set("sync", sync)

return msg;

```

So that is all working fine. What I could not get to work (I found a workaround but want to learn as thats important to know) is in the msg = { payload: (sync\_array.reduce... I also wanted to set payload.name. I tried various things, the best I managed was a name object. A lot of things I tried adding to that line to set payload.name ended in errors in the function. If I set it after the msg = {payload... then it deletes everything I set before...

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 December 2022 16:30 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/2 "2022-12-03T16:30:29Z")

</div>

I could be wrong in the interpretation here (just passing by 😅 )

But you are setting `payload` to the value of  
`(sync_array.reduce((acc, num) => acc + num, 0) + " Watts")`

it essentially looks like this.

```auto
{
   payload: "00.0 Watts",
   color: "lime",
   strongcolor: "deepSkyBlue",
   nodeName: "Plinth Lights" 
}

```

if you wanted to add `name` to `payload`, then you should do.

```auto
{
   payload: {
       value: "00.0 Watts",
       name: 'name here'
   },
   color: "lime"
   strongcolor: "deepSkyBlue",
   nodeName: "Plinth Lights" 
}

```

`msg.payload.name` = the name  
`msg.payload.value` = the value

`payload` needs to be an object {} to add further properties later

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [3 December 2022 16:38 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/3 "2022-12-03T16:38:25Z")

</div>

I was sure I tried your version... but obviously I didnt as this works. Too many {{{}}} got me confused... yeah that makes sense now. I thought it should be easy... Saved it to my library for future ref

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 December 2022 16:53 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/4 "2022-12-03T16:53:00Z")

</div>

any property can be anything (within reason 😅 )

`payload : "some string"` \< a string  
`payload : 10` \< a number  
`payload : {}` \< an object

objects supports properties or further objects

```auto
payload: {
    name: 'Some Name',
    some_data_object: {
      value: 10,
      name: 'some name'
    }
}

```

`msg` its self is an obejct

---

<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:** [3 December 2022 17:16 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/5 "2022-12-03T17:16:23Z")

</div>

> [@Mikee1234K](#):
>
> Too many {{{}}}

You can set the payload properties individually to avoid { } if you wish

```auto
let sync = context.get('sync') || {};
const nodeName = msg.nodeName;
sync[nodeName] = msg.payload.value;
const sync_array = Object.values(sync);
msg.payload = {};
if (sync_array.length >= 3) {
    msg.payload.value = sync_array.reduce((acc, num) => acc+num, 0)
    msg.payload.units = "watts";
    msg.payload.color = "lime";
    msg.payload.strongcolor = "deepSkyBlue";
    msg.payload.nodeName = "Plinth Lights";
    sync = undefined;
}else{
    msg = null;
}

context.set("sync", sync)
return msg;

```

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [3 December 2022 17:28 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/6 "2022-12-03T17:28:56Z")

</div>

Actually that looks a lot easier, also much easier to debug or change. I'll change my function but save both versions to my library for future ref

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [3 December 2022 17:44 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/7 "2022-12-03T17:44:48Z")

</div>

Actually, I just seen that is isnt working. I need the value in msg.payload, not payload.value

So I tried this:

```auto
let sync = context.get('sync') || {};
const nodeName = msg.nodeName;
sync[nodeName] = msg.payload.value;
const sync_array = Object.values(sync);
msg.payload = {};
if (sync_array.length >= 3) {
    msg.payload = sync_array.reduce((acc, num) => acc + num, 0).toFixed(1) + " Watts"
    msg.payload.name = "Test"
    msg.color = "lime";
    msg.strongcolor = "deepSkyBlue";
    msg.nodeName = "Plinth Lights";
    sync = undefined;
} else {
    msg = null;
}

context.set("sync", sync)
return msg;

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/cac555381dbc81bb791881ac7e80eb5e53af3562.png)

But that doesnt write anything into payload.name

---

<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:** [3 December 2022 17:47 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/8 "2022-12-03T17:47:17Z")

</div>

msg.payload can be a string or an object not both, what you are asking is impossible. Unless you use the new Quantum\_properties JS

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [3 December 2022 18:21 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/9 "2022-12-03T18:21:22Z")

</div>

Ok so the solution would be to have the value in payload.value then the name in payload.name. I would then set my graph to display payload.value instead of payload. Either way, I had it working anyway (I used nodeName instead of payload.name) and now understand why it would not let me set this, and you have shown me a easier way of setting various objects, so it was worth the exercise. I am slowly feeling a bit more confident programming the function node, I have done a few things now myself without help, but there is still a lot to learn. And I am sure there will be more questions in the future. I do look at the manual, sometimes I can follow what they mean, other times its just way over my head.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 December 2022 18:42 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/10 "2022-12-03T18:42:22Z")

</div>

Essentially.  
To extend on the answer given by @E1cid, A property can only be represented as 1 type.

- `string` -\> "Hello, World"
- `number` -\> 100
- `array` -\> ["Hello", "World"]
- `object` -\> {'property\_name': 'property\_value', 'another\_property\_name': 'another\_property\_value'}

therefore `payload` can only be one of these types

---

<div class="post-metadata">

**Author:** ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)\
**Post date:** [3 December 2022 18:44 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/11 "2022-12-03T18:44:54Z")

</div>

So that then explains, when I set payload to a value (or string), it cannot be a object payload.name at the same time. Clear.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 December 2022 18:46 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/12 "2022-12-03T18:46:24Z")

</div>

Correct!

`payload:"some string"` is a string, a string does not have properties.

however, an object does  
`payload: {}`

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 December 2022 18:59 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/13 "2022-12-03T18:59:53Z")

</div>

Bonus content! 😄

Thats not to say you can change the type (in most cases)

```auto
msg.payload = "Some String" /* payload is now a string */
msg.payload.name = 'bla' /* invalid */

```

```auto
msg.payload = {} /* payload is now an object */
msg.payload.name = 'bla' /* Valid */

```

---

<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:** [1 February 2023 19:00 UTC](https://discourse.nodered.org/t/trying-to-set-a-payload-name-in-a-function-node/71645/14 "2023-02-01T19:00:50Z")

</div>

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