Keeping original payload data while editing it

Flow json

{
  "data": 0
}

I feel like I'm trying something really simple and I can't seem to find an answer anywhere.
I have a simple http request test and all I need it to do it take the request. modify the data and store the original request and the modified data to a file, then have a way to read that file back. I have everything working, but I can't get the original payload data. Please help Thanks

Welcome to the forums @lafoscony

You can deep clone any object before modifying it...
The best way, is to use the change node.

This will create an object called backupPayload - this will create a deep cloned version of payload

Meaning edits to payload wont effect backupPayload
Place the change node before any nodes making changes to the payload

Its important to use Deep clone - as javascript is pass-by-ref, i.e if you don't deep clone, changes to payload - will also change backupPayload

When I do that it causes my function node to no longer work. It seems like the payload object just disappears. That was the first thing I tried, I just can't get it to work

Just to not the original incoming data is also in msg.req.body

The http request will be changing payload - that is normal.

so if you function node still needs data you sent.
change to using backupPayload inside off it (as it will be the original payload) before the http request

When I look there the variable is updated to the new one. I think it's referencing it when I modify it. I'm definitely doing something wrong, but I can't find any good documentation on how to do it

It is unclear as to what you want to see in the file and what you want to return to the response. please provide examples.

req.body is clearly still there

Have I missed something :thinking:

is this a HTTP Request - or HTTP IN?

It's an http in. I still can't get it to work with the change node. Even trying to get debug to print msg.backup returns nothing

I posted the flow on the original message. Hell I'd even let you login and look at this one. I'm just at a loss. I'm so confused right now

I don't know what else to provide. I gave the flow. I gave the original payload. and I said I would like it to just include the original payload with what it's already returning. I don't know how to do that. and when I look at the req data in the body it is the modified data, not the original

Without examples of the Object return to the response and what Object should go to the file, I am guessing now as I don't really fully get what you are saying.

let something = {};
something.data = msg.payload.data;


something.data += 1


msg.payload.body = {
        sourceSummary: something.data,
        errorNumbers: "errorNumbers",
        missingNumbers: "missingNumbers"
};

return msg;

I'm sorry I don't know how to explain any better. I send a post with
{ "data": 0 } I need to modify data lets say I just need to add one to it. I need to save to a file
{ "data": 0 } (the original request) and { sourceSummary: 1(data +1), errorNumbers: "errorNumbers", missingNumbers: "missingNumbers" }

Isn't that what my code above does?
output

{
  "data":0,
  "body":{ 
    "sourceSummary":1,
    "errorNumbers":"errorNumbers",
    "missingNumbers":"missingNumbers"
  }
}

you can then move delete any property at will.

Ok...

You're referring to HTTP Request (that is a Node) - hence my confusion :crazy_face:
(but I didn't have a look at the flow to be fair - so that is on me)

Directly after the HTTP IN node

  • Use a change node to store msg.payload in msg.original (use deep clone)
  • After you make changes to payload (for the client)
    • Respond (to client)
  • Then to store the original incoming data, and response to a file (after you respond)
  • use something like below
const Log  = {
  request: msg.original,
  response: msg.payload
}

return {payload:JSON.stringify(Log)}

Send the above function to the file wrote node?

Insurance: I still may have this wrong

It does do what I need, I just need it to only happen to the file that's being written, not in the actual response data itself

So i asked what the response and file should be several posts ago.

So what should go to response?
and what should go to file?

Any use of the change node makes it where I can't access original payload, or what i've changed it to

Because you have it the wrong way
you are setting payload