admin管理员组文章数量:1415484
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!
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!
Share Improve this question asked Aug 7, 2017 at 12:09 Денис ПархоменкоДенис Пархоменко 11 gold badge1 silver badge3 bronze badges 3-
Is it possible that the node reading the array from the context can get triggered before the one that writes it to the context. If so you need to add a test to see if the
context.get()
returnsundefined
(as it will if it's not been set yet) – hardillb Commented Aug 7, 2017 at 12:24 - Instead of assuming there are 15 elements, use acstate.length – SPlatten Commented Aug 7, 2017 at 12:24
- Sorry I didn't mention that I'm trying to write to context and to read from context from different nodes. So now I tried to change "context" to "flow" and it works! Thank you for answers! – Денис Пархоменко Commented Aug 7, 2017 at 16:29
3 Answers
Reset to default 1You 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;
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;
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!
本文标签: javascriptHow to store array in node red contextStack Overflow
版权声明:本文标题:javascript - How to store array in node red context - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745232394a2648883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论