2017-08-07 109 views
0

我試圖存儲和取回節點紅色上下文(或流)對象中的數組。如何在節點紅色上下文中存儲數組

我這樣做是爲了店裏,可以看到輸出消息數組:

var acstate=[]; 
for(var i=0;i<15;i++){ 
    acstate[i]=1; 
    } 
context.set("acstate",acstate); 
msg={'payload':acstate}; 
return msg; 

該節點從場景中獲得數組:

var acstate=[]; 
acstate = context.get('acstate'); 
for(var i=0;i<15;i++){ 
    node.warn(acstate[i]); 
    } 
msg={'payload':acstate}; 
return msg; 

它顯示

"TypeError: Cannot read property '0' of undefined" 

燦」 t找到有關存儲數組的信息,可以使用上下文嗎?如果不是,我可以使用什麼來保存數據?

謝謝!

+0

從上下文讀取數組的節點是否可以在寫入上下文之前被觸發。如果是這樣,你需要添加一個測試來查看'context.get()'是否返回'undefined'(如果它還沒有設置的話)。 – hardillb

+0

而不是假設有15個元素,請使用acstate.length – SPlatten

+0

對不起沒有提到我正在嘗試寫入上下文並從不同節點的上下文中讀取數據。所以現在我試圖改變「上下文」爲「流動」,它的工作原理!謝謝你的答案! –

回答

0

您可以編寫

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; 
0

你並不需要在分配給從另一個函數的返回之前創建數組:

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; 
0

對不起,我沒有提到我」 m試圖寫入上下文並從不同節點的上下文中讀取。看起來每個節點都有自己的上下文,這就是爲什麼存儲在一個節點上下文中的數據在另一個節點的上下文中不可用。所以現在我試圖改變「上下文」爲「流動」,它的工作原理!

var temp = flow.get('acstate'); 

謝謝你的回答!