Global memory updates even if I don't "save" back to it

Hi! Please help me understand the problem here.

I have this function node:

var Notifications = global.get("Active_Notifications")
delete Notifications.RearPumpFail.Acknowledged

In my mind, this should not change anything in global context memory. I haven't actually saved the "local" object back to the global context memory, e.g. by doing this: global.set("Active_Notifications", Notifications)

But it does update global context. When I inject a timestamp into this function, the context explorer sidebar shows the "acknowledged" value disappears. I have nothing connected to the output of the node, it's literally a standalone function node.

Can someone explain why it alters the content of memory even if I haven't "saved" my object back to memory? Is this specific to Node-RED's handling of objects / context, or is it inherent to OO programming in general?

While waiting for the experts to arrive, when this sort of thing happens in a language, it usually means that the variable assignment isn't making a copy of the object, its making a reference to the object.

So that means that anything you do to the variable is the same as doing it to the object itself.

Of course, this may not be the case here but it could explain why its happening

1 Like

@cymplecy thanks - looks like you are exactly right. I've just read this interesting article that goes into the subject in more detail.


As I said, I'm no expert but have a play using this

var Notifications = RED.util.cloneMessage(global.get(""Active_Notifications""));

Note - prob not THE way to do it but seems to work for me :slight_smile:

I see you solved this yourself so you can mostly ignore my comments but suffice to say, when you store and retrieve an object in global you are storing a reference to the original. So that said, when you update a property of that reference object you are modifying the original (and thus the object stored in context)

The tripping point here is since you are not using global.set('', new_value) (and instead using = new_value) persistent storage will not work. Not a major issue if you're not using persistent storage but it can catch you out (hint: the logic that persists to file is carried out inside the set(...) function).

It's just something you should be aware of when working with objects, references and context.

In summary, use global/flow .get and .set everywhere to be consistent.

Lastly if you did indeed want a "brand new unrelated" object then clone it before modifying. (Google is your friend here).