最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to store array in node red context - Stack Overflow

programmeradmin1浏览0评论

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() returns undefined (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
Add a ment  | 

3 Answers 3

Reset to default 1

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;

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!

发布评论

评论列表(0)

  1. 暂无评论