Reputation: 1
I'm trying to store and get back array in Node Red context (or flow) object.
I do this to store, can see output message with array:
var acstate=[];
for(var i=0;i<15;i++){
acstate[i]=1;
}
context.set("acstate",acstate);
msg={'payload':acstate};
return msg;
This node to get array from context:
var acstate=[];
acstate = context.get('acstate');
for(var i=0;i<15;i++){
node.warn(acstate[i]);
}
msg={'payload':acstate};
return msg;
It shows
"TypeError: Cannot read property '0' of undefined"
Can't find info about storing arrays, is it possible with context? If not, what can I use to keep data?
Thank you!
Upvotes: 0
Views: 11934
Reputation: 1
Sorry I didn't mention that I'm trying to write to context and to read from context from different nodes. Looks like each node has it's own context, thats why data stored in context of one node is not available in context of another node. So now I tried to change "context" to "flow" and it works!
var temp = flow.get('acstate');
Thank you for answers!
Upvotes: 0
Reputation: 5762
You don't need to create the array before assigning it to the return from another function:
var acstate; /* =[] NOT REQUIRED; */
acstate = context.get('acstate');
if ( typeof acstate == "object"
&& typeof acstate.length == "number"
&& acstate.length > 0 ) {
for(var i=0;i<acstate.length; i++){
node.warn(acstate[i]);
}
}
msg={'payload':acstate};
return msg;
Upvotes: 1
Reputation: 25
You can write like
var acstate=[];
var temp = context.get('acstate');
for(var x=0;x<temp.length;x++){
acstate.push(temp[x]);
}
for(var i=0;i<15;i++){
node.warn(acstate[i]);
}
msg={'payload':acstate};
return msg;
Upvotes: 1