How to automatically create global variables using an array

Hello all,

Newb here, trying to learn.

I have a node that will be passing down two string values that I want to assign to two different global variables, var1 and var2
I am having trouble with my code on how to do that with a for loop

This is what I got, which I know its wrong but I cant wrap my head around how to get this function to do that for me:

var SWSerial = ;
for (var i in msg.payload){
global.set(SWSerial[i], msg.payload.serial);
msg.serial = global.get("SWserial[0]");
return msg;}

Any guidance is greatly appreciated!!

The global path should be a string
e.g.

msg.serial = [];
for (var i in msg.payload){
global.set("SWSerial[" + i + "]", msg.payload[i].serial);
msg.serial.push(global.get("SWSerial[" + i + "]"));
}
return msg;

example flow

[{"id":"984f86ea.b649c8","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"serial\":45},{\"serial\":47},{\"serial\":48}]","payloadType":"json","x":90,"y":1320,"wires":[["705d73cd.e1255c"]]},{"id":"705d73cd.e1255c","type":"function","z":"30af2d3e.d94ea2","name":"","func":"msg.serial = [];\nfor (var i in msg.payload){\nglobal.set(\"SWSerial[\" + i + \"]\", msg.payload[i].serial);\nmsg.serial.push(global.get(\"SWSerial[\" + i + \"]\"));\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":1300,"wires":[["143dd92d.782eef"]]},{"id":"143dd92d.782eef","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":460,"y":1320,"wires":[]}]

or if separate global vars

msg.serial = [];
for (var i in msg.payload){
global.set("SWSerial" + i, msg.payload[i].serial);
msg.serial.push(global.get("SWSerial" + i));
}
return msg;

what about using a change node - no need for code in this that i can see ?

Craig

So I took your code and modified it a little to give me what I want, sort of
I have this:

for (var i in msg.payload) {
global.set("SWSerial" + i, msg.payload.serial);
msg.serial = global.get("SWSerial" + i);
}
return msg;

My problem now is calling each variableindividually. I try this to test:
for (var i in msg.payload) {
global.set("SWSerial" + i, msg.payload.serial);
}
msg.serial = global.get("SWSerial[0]");
return msg;

Now., both variables are the same value. Not sure how moving that outside of the for loop broke that.
So even if I try [0] or [1], I get only one of the two switches serial number.

I feel like its something so simple now but Ive tried different variations and cant seem to get it.

You are not following the suggestion from @E1cid

If you use this:

global.set("SWSerial" + i, msg.payload.serial);

you are saving separate variables, like SWSeria1, SWSerial2, etc..

If you want an array, you should follow this other syntax:

msg.serial = [];
for (var i in msg.payload){
global.set("SWSerial[" + i + "]", msg.payload[i].serial);
msg.serial.push(global.get("SWSerial[" + i + "]"));
}
return msg;
1 Like

As @fmarzocca says, if you store them as an array you can recall the whole array

for (var i in msg.payload) {
global.set("SWSerial[" + i +"]", msg.payload[i].serial);
}
msg.serial = global.get("SWSerial");
return msg;

// or using forEach()
msg.payload.forEach((obj, index) => {
    global.set("SWSerial[" + index + "]", obj.serial);
})
msg.serial = global.get("SWSerial");
return msg;

Also you get the same serial in both because you save the same value msg.payload.serial. I am guessing msg.payload should be an array as i have shown in my examples.

If this is not correct please supply an example of msg.payload and exactly how you wish SWSerial global to be stored.

1 Like

Ok so now that I think about it more, I don't think it matters how I accomplish this, meaning, through an array or through separate variables, my goal is to be able to call one of my switches serial, whether thats using an array (SWSerial[0]), or completely different variables (SWSerial1, SWSerial2, etc.)
Ultimately, I imagine I would be able to use the array to create the distinct variables if I wanted to.
Hope that makes sense.

I tried:

for (var i in msg.payload) {
global.set("SWSerial[" + i +"]", msg.payload[i].serial);
}
msg.serial = global.get("SWSerial");
return msg;

And that gave me:
Error: Invalid property expression: unexpected n at position 9

To add more color, this function is receiving two objects, both with identical attributes, different values. One of those being msg.payload.serial.

I hope that helps, and I appreciate you guys taking the time on this to help me figure it out.

You need to put some debugging nodes (and name them) on your msg flow and post up a picture of the flow and the outputs from the Debug nodes so we can see exactyl what the messages look like please

Craig

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