简体   繁体   English

如何在节点红色上下文中存储数组

[英]How to store array in node red context

I'm trying to store and get back array in Node Red context (or flow) object. 我正在尝试在Node Red上下文(或流)对象中存储并获取数组。

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! 谢谢!

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! 谢谢您的回答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM