Manipulating Data form Global Object

Hello everyone,

I have got a strange issue.
I would like to take data (array) from a global variable object. Replace the null values with another value and send the result to another node by msg.payload. Sounds easy.
BUT when I get the data from the variable it seems like it's not possible to change the values. The nulls are still in the array. When I create the same data in another variable by hand everything works just fine. When I just get the data from the variable and save it to another variable. It doesn't work out with the new variable either.
I also tried to get the data with RED.util.cloneMessage() but it doesn't change the outcome.
The variable containing the data I want to manipulate is changed all the time. Maybe there is a correlation.

Thank you for your help.

Sincerely Thomas

can you show us the flow doing this? Export it and add it to a reply

1 Like

(You beat me to the post)

The project is kind of complex and needs multiple csv files to work. But I will try to recreat the problem in a simple flow.

Show us the function you are using, with the cloneMessage() call included. It does sound as if your modified object is being overwritten when the global array is updated.
Use the </> button and paste the function in where it says.

var data_ist = RED.util.cloneMessage(global.get("prozesstemp_global_object_2").ist_verlauf[2]);

for (let i in data_ist){
    if (data_ist[i] === null){
        if (i >= 1){
            if (data_ist[i-1] !== null){
            var lower_value = data_ist[i-1];
            data_ist[i] = lower_value ;

msg.payload = data_ist;
return msg;
[{"id":"35096b3b.a84954","type":"inject","z":"dd2b1b78.857558","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":580,"y":400,"wires":[["71a5b799.900de8"]]},{"id":"b71d41f5.a5bf6","type":"debug","z":"dd2b1b78.857558","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":990,"y":400,"wires":[]},{"id":"71a5b799.900de8","type":"function","z":"dd2b1b78.857558","name":"remove null","func":"var data_ist = RED.util.cloneMessage(global.get(\"prozesstemp_global_object_3\").ist_verlauf[2]);\n//var data_ist_original = RED.util.cloneMessage(global.get(\"prozesstemp_global_object\").ist_verlauf[2]);\nvar msg2= {};\n\nfor (let i in data_ist){\n    if (data_ist[i] === null){\n        if (i >= 1){\n            if (data_ist[i-1] !== null){\n            var lower_value = data_ist[i-1];\n            data_ist[i] = lower_value ;\n            }\n            \n        }\n    }\n}\n\n/*for (let i in data_ist_original){\n    if (data_ist_original[i] === null){\n        if (i >= 1){\n            if (data_ist_original[i-1] !== null){\n            lower_value = data_ist_original[i-1];\n            data_ist_original[i] = lower_value ;\n            }\n            \n        }\n    }\n}*/\n\nmsg.payload = data_ist;\n//msg2.payload = data_ist_original;\nreturn [msg,msg2];","outputs":2,"noerr":0,"x":770,"y":400,"wires":[["b71d41f5.a5bf6"],["478a382a.0a8808"]]},{"id":"ff633fc3.02bc3","type":"debug","z":"dd2b1b78.857558","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":990,"y":340,"wires":[]},{"id":"717741a1.ff4d4","type":"function","z":"dd2b1b78.857558","name":"create object","func":"function getRandomInt(min, max) {\n  min = Math.ceil(min);\n  max = Math.floor(max);\n  return Math.floor(Math.random() * (max - min)) + min;\n}\n\nvar prozesstemp_global_object_3 = global.get(\"prozesstemp_global_object_3\")||{\n    liste_maschinen_ID : [],\n    timestamp_verlauf : [],\n    soll_verlauf :[],\n    max_verlauf :[],\n    min_verlauf :[],\n    ist_verlauf :[[],[],[]],\n    timestamp_on:[],\n    status: [],\n    prozessnummer:[],\n    filenames: [],\n};\n\nvar value_in = getRandomInt(0, 10);\n\nif (value_in <= 3){\n    value_in = null;\n}\n\n\nvar ist_data = prozesstemp_global_object_3.ist_verlauf[2];\n\nist_data.push(value_in);\n\n if(ist_data.length >= 20){\n        ist_data.pop();\n        }\n        \nprozesstemp_global_object_3.ist_verlauf[2] = ist_data;\n\nglobal.set(\"prozesstemp_global_object_3\",prozesstemp_global_object_3);\n\nmsg.payload = ist_data;\n\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":340,"wires":[["ff633fc3.02bc3"]]},{"id":"206b9e82.73ef82","type":"inject","z":"dd2b1b78.857558","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":590,"y":340,"wires":[["717741a1.ff4d4"]]},{"id":"478a382a.0a8808","type":"debug","z":"dd2b1b78.857558","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":990,"y":440,"wires":[]}]

So I recreated the Situation but I work just fine with the new object. I don't know why...

That looks ok. If you feed that directly into a debug node does it show the replaced data?
If not then add this line after data_ist[i] = lower_value ;

node.warn(`Setting data_ist[${i}] to ${lower_value}`)

and see what it says in the debug pane
I am not sure whether for/in is a recommended way of iterating an array, but it does seem to work for a simple array. It is normally used for iterating properties.

[Edit] Just had a thought, if the elements you are testing for null don't exist at all in the array then I don't think for/in will select them, so perhaps that is the cause.

1 Like

That's a great tip! I don't think they do exist. How can I catch them anyway?

Start by using for (i = 0; i < data_ist.length ; i++) { which will give you an iteration even if the element doesn't exist, then I think that rather than using === null it might be better with == null as that, I believe, will also catch undefined.
Add the warn as I suggested then you can check it is working.

As a general debugging point, when you have something not working it is always best to check which bits are working and which bits are not before jumping to a conclusion about what is happening, so add debug along the chain to find the first point of failure.

1 Like

Thank you very much! Not knowing how exactly a for in loop works cost me at least 5 hours of my life....

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.