Function node not possible to store global array variable

Hi all,

I have a function node with the Form node in front in DB2 and I would like to save it to global variable (global.set). But nothing happens … script is got from DB1, is there a possibility, that Form node put values different, but I don’t know why, because DB1 was uninstalled from my NR.

Here is the code :

var userdata = global.get("userdata") || [];
userdata.push=({
id:msg._client.socketId,
ns:msg.payload.name_surname,
login:msg.payload.user_login,
pass:msg.payload.user_pass,
rights:msg.payload.user_rights
});
global.set("userdata",userdata);
msg.payload=userdata;
return msg;

And flow looks :

And result is :

obrázok

Thanks a lot for help.

You are destroying the push function by assigning a value to it.

Remove the =

@Steve-Mcl so there is an error after removing = :

Put a debug node on the output of your ui-form node to check what you are getting out of it.

@Buckskin screen from directly connected debug :

Problem solved :

var userdata = [];
var read_data = global.get("userdata");
if (read_data){
userdata = read_data;
}    
userdata.push({
id:String(msg._client.socketId),
ns:msg.payload.name_surname,
login:msg.payload.user_login,
pass:msg.payload.user_pass,
rights:msg.payload.user_rights
});
global.set("userdata",userdata);
msg.payload=userdata;
return msg;

Problem was identified after first wrong data entry into global.set. Next error rise up immediately after it. After deleting global variable, and change header Vars with if condition …. if is empty, create new, and if is not empty, add previous data and push new to this.

Thanks guys to correct direction and also to making me think about it.

Bobo

If there is a chance of random data being in userdata then a better option would be;

let userdata = global.get('userdata')

if (!Array.isArray(userdata) ) userdata = []

The only time this would fail is if there was a naff Array in the global 'userdata'

Of course, thanks to this point. is Array is good solution.
I have changed it …. and working good. Thanks.
I think in global var will not “stay” any fail.