# Function node \> msg.payload versus payload?

**URL:** https://discourse.nodered.org/t/function-node-msg-payload-versus-payload/44589
**Category:** General
**Created:** [23 April 2021 09:13 UTC](https://discourse.nodered.org/t/function-node-msg-payload-versus-payload/44589 "2021-04-23T09:13:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)
#### Post date: [23 April 2021 09:13 UTC](https://discourse.nodered.org/t/function-node-msg-payload-versus-payload/44589/1 "2021-04-23T09:13:43Z")

</div>

Hi,

I'm quite new to node-red and also JS, but for the moment I'm quite happy with what I could achieve ( I focus on making workflows and custom panels for video broadcast devices)  
I try to make use as much as possible of function node, as it gives me most control on the code , just using change nodes when I (believe I) don't have an easy alternative.  
Problem I have in fucntion node quite often, is dealing with arguments coming into the node . Example :  
if I use context.set("test", msg.payload.XXXX); I get a error telling XXX is undefined.

Now, if I use :

payload = msg.payload;

then

context.set("test", payload.XXXX) : I'm fine .

Is that the expected behaviour ? I guess so, I miss something there for a full understanding....

Thanks !

Jerome

---

<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: [23 April 2021 09:22 UTC](https://discourse.nodered.org/t/function-node-msg-payload-versus-payload/44589/2 "2021-04-23T09:22:52Z")

</div>

> [@JeromeVISUALS](#):
>
> Example :  
> if I use context.set("test", msg.payload.XXXX); I get a error telling XXX is undefined.
> 
> Now, if I use :
> 
> payload = msg.payload;
> 
> then
> 
> context.set("test", payload.XXXX) : I'm fine

In a function node, `msg` is the incoming object. `payload` is a sub property of `msg` If msg.payload.XXXX throws that error then it simply doesn't exist in the `msg.payload`. something else must be going on. E.g. you are sending a different msg or you overwrite something before that line.

Posting your full function and details of what goes into it would help us help you.

Ps, both versions of your code should work (however to minimise bugs, you should define the `payload` variable using `let` or `const`)

---

<div class="post-metadata">

### Author: ![JeromeVISUALS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jeromevisuals/32/40103_2.png) [@JeromeVISUALS](https://discourse.nodered.org/u/JeromeVISUALS)
#### Post date: [24 April 2021 09:30 UTC](https://discourse.nodered.org/t/function-node-msg-payload-versus-payload/44589/3 "2021-04-24T09:30:02Z")

</div>

Hi,

thanks for your answer. I still don't really understand, as I explained in first post the msg.payload being assigned to a "payload" variable ( could be anything else there) is the only difference between working and not-working code....

I'll investigate.

Jerome

---

<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: [24 April 2021 11:01 UTC](https://discourse.nodered.org/t/function-node-msg-payload-versus-payload/44589/4 "2021-04-24T11:01:37Z")

</div>

As I said, post your full function and details of what goes into it.

To be 100% clear...

`msg.payload.XXXX`

and

```auto
var payload = msg.payload;
payload.XXXX;

```

are exactly the same thing. No two ways about it.

---

<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: [23 June 2021 11:02 UTC](https://discourse.nodered.org/t/function-node-msg-payload-versus-payload/44589/5 "2021-06-23T11:02:37Z")

</div>

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