# Issue setting payload in function

**URL:** <https://forums.losant.com/t/issue-setting-payload-in-function/4839>\
**Category:** Help\
**Tags:** function\
**Created:** [December 9, 2022, 11:46pm UTC](https://forums.losant.com/t/issue-setting-payload-in-function/4839 "2022-12-09T23:46:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paolo\_Proxy](https://avatars.discourse-cdn.com/v4/letter/p/b5ac83/32.png) [@Paolo\_Proxy](https://forums.losant.com/u/Paolo_Proxy)\
**Post date:** [December 9, 2022, 11:46pm UTC](https://forums.losant.com/t/issue-setting-payload-in-function/4839/1 "2022-12-09T23:46:03Z")

</div>

Hi there,  
I have a function with a scope payload and I want to set the result.

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/5/5e51244b810c88ae0329348244b3a255775f4c1c.png)

I am confused why this is now working?

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [December 10, 2022, 3:31pm UTC](https://forums.losant.com/t/issue-setting-payload-in-function/4839/2 "2022-12-10T15:31:44Z")

</div>

```javascript
payload.data = rows;

```

This line is _mutating_ the context passed in to the node; I would expect that, after this node runs, the payload value of `data.buildingForm.body.content.data` would be the array `rows` that you are building in this Function Node.

```javascript
//return rows;

```

If you were to comment the mutation line and uncomment this line, then I would expect that the value of `data.buildingForm.body.content` would be the `rows` array. (Note the lack of a `data` property inside `content` here.)

Are you seeing behavior different from what I am describing above?

The [Function Node documentation](https://docs.losant.com/workflows/logic/function/#returning-a-new-payload) covers this; I recommend checking that out for more information.

---

<div class="post-metadata">

**Author:** ![Paolo\_Proxy](https://avatars.discourse-cdn.com/v4/letter/p/b5ac83/32.png) [@Paolo\_Proxy](https://forums.losant.com/u/Paolo_Proxy)\
**Post date:** [December 13, 2022, 7:12am UTC](https://forums.losant.com/t/issue-setting-payload-in-function/4839/3 "2022-12-13T07:12:55Z")

</div>

Yes the behaviour is different: the data.buildingForm.body.content.data does not contain the rows. Maybe it has something to do with the scoping mechanism?

The line commented out is working as expected from the documentation, I was using that to make sure the rows object is full.

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [December 13, 2022, 3:20pm UTC](https://forums.losant.com/t/issue-setting-payload-in-function/4839/4 "2022-12-13T15:20:59Z")

</div>

I think what’s happening is this: The value you are passing in as the Function Node scope (`data.buildingForm.body.content`) is an array, and you are attempting to set a named property of `data` onto that array, which is then lost when serializing that payload back into JSON. Here’s a [good article](https://stackoverflow.com/questions/20270345/why-do-arrays-behave-this-way-when-i-add-named-properties) describing the issue.

If what you’re after is replacing the Function Node’s passed-in scope with the `rows` array, you have two options:

```javascript
payload = rows; // note it's not payload.data
// OR
return rows;
```
