I grab this table in a function in order to feed a HTML table to show status and time of last update in a easy to read form. Thus:
var dlist = global.get("devices")
for (const d of dlist) {
node.warn(d);
// loop over objects in dlist
var tstamp = new Date(d.time)
var tstring = tstamp.toLocaleString('de-DE')
d.time = tstring;
d.color = "white"
if (d.status == "offline") {
d.color = "red"
}
}
msg.payload = dlist
return msg
As you can see there is no global.set function called. Nevertheless when runnning the function the global variable devices is changed. The time stamp is now a string.
I already thought about a "by reference" issue. Actuall this is the first time I want a function to get a global variable and modify it.
I tried the RED.util.cloneMessage function but it throws an array in the loop:
for (const d of dlist)
var dlist: object
Type 'object' must have a '[Symbol.iterator]()' method that returns an iterator.(2488)
as suddenly dlistis no longer an array but an object.
Ok that's interesting:
The global variable devices is an array of objects (filled by another function)
Getting the content into dlist results in an object with one array!
Node.warn output:
Please look at my first posting. That is the content of the global variable devices
And that is array of objects.
In practice it would not really matter as long as I can loop over this array to modify each object in dlist and return an output array without modifying the global variable
Actually this works now! The javascript editor still reports the error message (see above) but the function returns the desired array and does not modify the global variable!
let dlist = global.get("devices")
let result = []; // <== create a new array for results
for (const d of dlist) {
let dd = RED.util.cloneMessage(d); // <== clone the current object
node.warn(dd);
// loop over objects in dlist
let tstamp = new Date(dd.time); // <== begin to modify the newly created object
let tstring = tstamp.toLocaleString('de-DE');
dd.time = tstring;
dd.color = "white";
if (dd.status == "offline") {
dd.color = "red";
}
result.push(dd); // <== push the modified object to the results array
}
msg.payload = result; // <== forward the results of your operation
return msg