Issue setting payload in function

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

I am confused why this is now working?

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.

//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 covers this; I recommend checking that out for more information.

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.

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 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:

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