Delete is affecting the flow.variables

#1

Hello All

I have this following conundrum

Consider that I have the following flow variable

image

and the following function node

Tudo =flow.get('firstFloorState');

delete Tudo["temp.guest"];
delete Tudo["temp.girl"];
delete Tudo["temp.master"];
delete Tudo["temp.boy"];
delete Tudo["temp.sala"];
delete Tudo["temp.ext"]
msg.payload=Tudo
return msg;

now my Flow variables are

image

Should this function without any flow set, alter the variables?

PS:

I am using the V19.2
Thank you

0 Likes

#2

Yes - it isn't ideal, but its how its always worked. The 'get' is returning a reference to the object in memory. The delete operates on that object, so modifies the object in context.

But you have to be careful. If you're using a persistent context, rather than in-memory, if you don't do the 'flow.set' call, the updates to the object will not be saved.

0 Likes

#3

Understood

Is there another way around way to accomplish the removal of the object?

JLG

0 Likes

#4

If you mean you want to retrieve the context value and only pass on certain properties, you can either:

  1. clone the context object so you can modify it in place

    var Tudo = RED.util.cloneMessage(flow.get('firstFloorState'));
    delete Tudo['temp.guest'];
    delete ...
    
  2. create a new object locally and copy in the values you do want

    var Tudo = flow.get('firstFloorState');
    msg.payload = {
        "Luz.Master": Tudo['Luz.Master'],
        ....
    }
0 Likes

#5

Thank you!!!

0 Likes