ndarkness ndarkness - 4 months ago 32
Node.js Question

Best way to store/initialize a persistant variable in Node-Red block?

I am using node-red and I would be interested on making a persistant varible ( atcually an array), that is not deleted each time that data reached a node. I have read about context option but I cannot manage to make it work.

The point is that I have a block where I woould like to store some incoming data, so later I can compare that stored data with the new incoming one. However, if I define a variable in such a block, that variable will be overwritten each time that something arrives to the block itself, so I cannot effectively compare.

How can I do that then? And, what/where is the best way to initialize that variable?

Thanks in advance,

Best regards


The context functionality is exactly what you want.

There are many ways you can work with context properties. You can get/set them with the Change node or do it with the Function node.

For example:

var myArray = flow.get('myArray');
if (!myArray) {
    myArray = [];

// do something with myArray

// save it back to context

The documentation has some more details: http://nodered.org/docs/writing-functions#storing-data