# \_msgid being saved to object in context

**URL:** <https://discourse.nodered.org/t/msgid-being-saved-to-object-in-context/16016>\
**Category:** General\
**Created:** [27 September 2019 21:19 UTC](https://discourse.nodered.org/t/msgid-being-saved-to-object-in-context/16016 "2019-09-27T21:19:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![m\_elias](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@m\_elias](https://discourse.nodered.org/u/m_elias)\
**Post date:** [27 September 2019 21:19 UTC](https://discourse.nodered.org/t/msgid-being-saved-to-object-in-context/16016/1 "2019-09-27T21:19:25Z")

</div>

I have a simple inject-\>function flow to recreate an object in the flow context for when I mess it up in my other flows. For some reason, it saves a \_msgid to that object. It's the same \_msgid as the function node automatically adds when passing to the next debug node. I have to add a change node to delete it. Is this just on my system?

```auto
[{"id":"4e3642eb.1c771c","type":"inject","z":"893c9b3d.b9d948","name":"","topic":"","payload":"","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":440,"wires":[["3adbd3d0.43b54c","f8562bc.8e668d8"]]},{"id":"3adbd3d0.43b54c","type":"function","z":"893c9b3d.b9d948","name":"create default fans context object","func":"var fanList = ['5700SmallFans', '11kFan', '6500Fan', 'e4kFan'];\nvar fans = {};\n\nfor (let index in fanList){\n fans[fanList[index]] = {};\n fans[fanList[index]].deviceName = fanList[index];\n fans[fanList[index]].autoEnabled = 0;\n fans[fanList[index]].autoMode = 0;\n fans[fanList[index]].autoModeDesc = \"cooling\";\n fans[fanList[index]].autoTemp = 15;\n fans[fanList[index]].autoRun = 0;\n}\n\nflow.set('fans', fans);\nnode.warn(fans);\n\nreturn fans;","outputs":1,"noerr":0,"x":320,"y":440,"wires":[["5160fe8a.54c47"]]},{"id":"5160fe8a.54c47","type":"debug","z":"893c9b3d.b9d948","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":530,"y":440,"wires":[]},{"id":"f8562bc.8e668d8","type":"change","z":"893c9b3d.b9d948","name":"","rules":[{"t":"delete","p":"fans._msgid","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":480,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![m\_elias](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@m\_elias](https://discourse.nodered.org/u/m_elias)\
**Post date:** [27 September 2019 21:21 UTC](https://discourse.nodered.org/t/msgid-being-saved-to-object-in-context/16016/2 "2019-09-27T21:21:52Z")

</div>

Returning null from my function node fixes it, but I still don't understand how it gets saved to the flow context.

---

<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:** [27 September 2019 21:24 UTC](https://discourse.nodered.org/t/msgid-being-saved-to-object-in-context/16016/3 "2019-09-27T21:24:24Z")

</div>

```auto
flow.set('fans', fans);
node.warn(fans);

return fans;

```

This places the `fans` object into Context. You then return a reference to the same object to the next node. That message gets an id assigned to it - but as it is a reference to the object you have put into context, you are modifying the same thing in memory.

---

<div class="post-metadata">

**Author:** ![m\_elias](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@m\_elias](https://discourse.nodered.org/u/m_elias)\
**Post date:** [27 September 2019 21:30 UTC](https://discourse.nodered.org/t/msgid-being-saved-to-object-in-context/16016/4 "2019-09-27T21:30:10Z")

</div>

Why does the fans object in the function become a reference for the fans object in Context? Does it happen with **flow.set** or **return fans**? Do I need to copy it to Context differently? I expected the two objects to be completely separate. BTW I'm enjoying Node Red, but I'm just learning as I go here. Sometimes just enough to be dangerous 🤪

---

<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:** [27 September 2019 21:31 UTC](https://discourse.nodered.org/t/msgid-being-saved-to-object-in-context/16016/5 "2019-09-27T21:31:48Z")

</div>

> [@m\_elias](#):
>
> Why does the fans object in the function become a reference for the fans object in Context?

It doesn't become a reference... it never stops being a reference. You put the 'fans' object into context and then you pass the same object on.

You can clone it before returning to break the link:

```auto
return RED.util.cloneMessage(fans);

```
