# Msg.payload is changing within a node

**URL:** https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311
**Category:** General
**Created:** [14 August 2019 10:42 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311 "2019-08-14T10:42:05Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)
#### Post date: [14 August 2019 10:42 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/1 "2019-08-14T10:42:05Z")

</div>

Hi,  
i m struggling with some code function in node red.  
if you can have a look on please

so the principle is that i want to draw a graph with values correspoding to certain topics.

so first i read a csv file to know which topics i need to record and i store them in an array "SelectedTopics", this is coming from a previous flow and from a global variable msg1

then i receive some topics from a MQTT Broker, now i want to take into account only the topics available in SelectedTopics.

the problem is that, when i check if the topic received is within the list of my selectedTopics, my msg.payload got a new value which is not the one from my MQTT broker but rather the SelectedTopics.payload.  
i don t want this to change, so how to avoid it

here is a my code

```auto
var SelectedTopics=[];
SelectedTopics=global.get("msg1")
 
 
 var msgs = [];
 
 var time="";

//get the timestamp name

 for(var attr1 in msg.payload)
      {
          // verify the 3 last characters
          if(attr1.substr(attr1.length - 3)=="_ts" )
          {
          time=attr1;
          }

      }
      
      
 for(var attr in msg.payload) //get each message attribute
      {
         
         
          
          // verify that the attribute is one of those we want to record
      
         if (SelectedTopics.hasOwnProperty(attr)) // when i do this my msg.payload change and take the value of SelectedTopics , want to avoid this and keep the msg.payload coming from my broker
          {
              
        // construct an array containing each attribute with its payload value and set a topic name for this
          var newmsg={};
          newmsg.topic=attr;
          node.warn(`topic is ${attr}`)
          newmsg.payload=msg.payload[attr]
          node.warn(`payload value is ${msg.payload[attr]}`)
          newmsg.timestamp=msg.payload[time];
          node.warn(`timestamp is ${msg.payload[time]}`)
          
          msgs.push(newmsg);
          
         
         }
          
    }

    return [msgs];

```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [14 August 2019 10:45 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/2 "2019-08-14T10:45:05Z")

</div>

Hi @Reims

when sharing code on the forum, please try to format it properly. Either use the `</>` button in the format toolbar, or surround your code with three backticks:

> ```  
> your code here  
> ```

Please edit your post and then we can read your code properly and help.

---

<div class="post-metadata">

### Author: ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)
#### Post date: [14 August 2019 11:06 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/4 "2019-08-14T11:06:21Z")

</div>

sorry Knolleary,  
did not know first, i edited my post

---

<div class="post-metadata">

### Author: ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)
#### Post date: [14 August 2019 11:58 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/5 "2019-08-14T11:58:36Z")

</div>

Any help please?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [14 August 2019 12:13 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/6 "2019-08-14T12:13:29Z")

</div>

We aren't sat in front of our computers endlessly refreshing the page to check if there is something to reply to. Please be a little patient.

Can you provide more of an example of the messages you pass to this function and what you expect the result to be. It is hard to picture what it is meant to be doing, nor what issue you are seeing.

---

<div class="post-metadata">

### Author: ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)
#### Post date: [14 August 2019 12:29 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/7 "2019-08-14T12:29:33Z")

</div>

sorry for my impatience...

More details

my SelectedTopic contains this value

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

and my msg.payload contains this value

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f9925ec27c7c38f051333efd38c9a0ea19445da9.png)

so what i need concretly to do is to return the attributes and value from my msg.payload only if this attribute is one of the selected topic.

in this case i only need to return test\_ax:5, test\_ay:1.3 and test\_rx:1.3

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [14 August 2019 12:35 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/8 "2019-08-14T12:35:55Z")

</div>

> [@Reims](#):
>
> in this case i only need to return test\_ax:5, test\_ay:1.3 and test\_rx:1.3

And do you want to return them as a single message containing all of the matching attributes, or one message per attribute?

---

<div class="post-metadata">

### Author: ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)
#### Post date: [14 August 2019 12:37 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/9 "2019-08-14T12:37:20Z")

</div>

> [@knolleary](#):
>
> as a single message containing all of the matching attributes,

like this

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [14 August 2019 12:41 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/10 "2019-08-14T12:41:12Z")

</div>

Your current code appears to create a new message for each matching attribute.

But if you want a single message with the matching attributes left in `msg.payload`, then something like the following would do it:

```auto
var SelectedTopics = global.get("msg1");

var newPayload = {};

SelectedTopics.forEach(function(attribute) {
    newPayload[attribute] = msg.payload[attribute]
})

msg.payload = newPayload;

return msg;

```

---

<div class="post-metadata">

### Author: ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)
#### Post date: [14 August 2019 12:49 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/11 "2019-08-14T12:49:57Z")

</div>

got this from my debugging node

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6a5cf63761a60cfd9f49a2c7d1af720997fd292c.png)

and still , i think is is always changing my msg.payload value.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [14 August 2019 12:53 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/12 "2019-08-14T12:53:56Z")

</div>

Is that using the code I provided? Or your original code?

What exactly is stored under `msg1` in Global context? From your code I assumed you were storing an array. But maybe not.

You can use the Context sidebar to see the contents of context to confirm exactly what you have.

> [@Reims](#):
>
> i think is is always changing my msg.payload value.

If you are using the code I provided and it hit the error you show, then it would have stopped. You must have something else in your flow changing the payload or you are not looking at what you think you are.

---

<div class="post-metadata">

### Author: ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)
#### Post date: [14 August 2019 13:03 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/13 "2019-08-14T13:03:11Z")

</div>

> [@knolleary](#):
>
> that using the code I provided

yes using the code you provided,

i have 2 flows, the first flow reads a csv file and save the topics i need to record into the global variable msg1

my csv file looks like this

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

and the function i use to take only the value i need to record is this

```auto
var out = [];

msg1={};

for (var i = 0; i < msg.payload.length; i++) {
    if (msg.payload[i].col2 !==null && msg.payload[i].col2=="x")
    {

      
         var tmp=msg.payload[i].col1;
         out.push(tmp);
         
       
    }
}

msg1.payload = out;
global.set("msg1",msg1); 
return msg1;

```

then in my second flow, i have a MQTT broker which is executing a dummy python code to send some topic with their value

```auto
pub1 = mqttPublisher(topic="test/test_HF", name="cansensor")
print("Dummysender running")

while True:
    time.sleep(1)
    mm05Dict = {
        'test_ts': round((time.time()) * 1000),
		#'test_ts': round(time.time()),
        'test_ax': randrange(10),
        'test_ay': randrange(10),
        'test_az': randrange(10),
        'test_rx': 1.3,
        'test_rz': 1.5
    }

```

now i want just to take the value of a topic if this topic is within the list of the selected topic i took from my csv file, that is why i m using the if

but when i m doing this, it seems like the msg.payload i got from the MQTT broker is erased and remplaced by the msg1

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [14 August 2019 13:16 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/14 "2019-08-14T13:16:26Z")

</div>

So `global.msg1` is not an array of strings. It is an object with a payload property whose contents is an array of the topics you want.

In which case, you can change the first line of the function to:

```auto
var SelectedTopics = global.get("msg1.payload");

```

> [@Reims](#):
>
> but when i m doing this, it seems like the msg.payload i got from the MQTT broker is erased and remplaced by the msg1

What does your actual flow look like?

The function you've shared that stores `msg1` in global context does send on a message with a payload containing the array of strings. Could it be _that_ message you are seeing?

---

<div class="post-metadata">

### Author: ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)
#### Post date: [14 August 2019 13:18 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/15 "2019-08-14T13:18:34Z")

</div>

> [@knolleary](#):
>
> So `global.msg1` is not an array of strings. It is an object with a payload property whose contents is an array of the topics you want.

yes exact,

and obviously it was the msg1 that was returned in the second flow,

i m not sure what solved this but i just remove the return from my first flow. and now i got the correct value i wanted

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [14 August 2019 15:10 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/16 "2019-08-14T15:10:01Z")

</div>

If you are storing a complete message in context then don't do that. It is dangerous as messages are stored by reference, so if you save a message to context and then pass it on and, for example, the payload gets changed by another node then the payload in the context will also get changed, that can cause very odd effects. Just save to context the bit you need - the array in this case I think.

---

<div class="post-metadata">

### Author: ![Reims](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/reims/32/10737_2.png) [@Reims](https://discourse.nodered.org/u/Reims)
#### Post date: [14 August 2019 15:26 UTC](https://discourse.nodered.org/t/msg-payload-is-changing-within-a-node/14311/17 "2019-08-14T15:26:11Z")

</div>

Thanks.  
i updated it and just save the array.
