I am trying to build a function node that takes this input from a switch node:
topic: "Name of Alarm" payload: 1
I am trying to create an array within a function node that pushes the topic inputs, into an array. This is the code I have in my function node so far:
context.outputAlarms = [];
context.outputAlarms.push(msg.topic);
msg.payload = context.outputAlarms;
return msg;
And this is the debug node output: C-1A SEL FOR RECV SYS AUTO STP : msg : Object { "payload": [ "C-1A SEL FOR RECV SYS AUTO STP" ], "topic": "C-1A SEL FOR RECV SYS AUTO STP", "_msgid": "bbe3efc0.3816a" }
I'm using "context" because from what I understand it is a module that stores data in that certain node. I believe my issue is that the array isn't adding new items, it is overwriting them. Can anyone point me in the right direction?
Your first line is overwriting the array with an empty array each time the function runs. You need to test if it already exists and only set it to empty if not.
Something like this should work:
if (!context.outputAlarms) {
context.outputAlarms = [];
}
context.outputAlarms.push(msg.topic);
msg.payload = context.outputAlarms;
return msg;
Also please read the doc about using the context, the example you are using is the old deprecated method to access it. The docs are here