Problem with global.get in a function

I tried several hours to work with a global (and persistant) variable. After many tries i realized, that everything works fine, when i'm using context, saved as memoryOnly, but it doesn't if i try to use a global context wich is saved to file...

I've followed the NodeRed docs and used this in my settings.js:

contextStorage: {
   default: "memoryOnly",
   memoryOnly: { module: 'memory' },
   file: { module: 'localfilesystem' }
}

If i now create a global Variable with this function:

global.set("lightsPower",msg.payload);
return msg;

i can read and use it like this:

var M = global.get("lightsPower");
msg.payload = M;

return msg;

But if i create it with a change-node and select file instead of memoryOnly as destination (?!) i can't get it, no matter what i tried.
Do i need to point to the filesystem in anyway?

Have you re-started node-RED since editing the settings.js file? It needs a restart to read in the new setting.

What do you get with this?

If you now create a global Variable with this function:

global.set("lightsPower",msg.payload, "file");
return msg;

can you read and use it like this:

var M = global.get("lightsPower", "file");
msg.payload = M;
return msg;

It is a bit hidden, as the info you required is in the docs under Writing Functions Guide.
https://nodered.org/docs/user-guide/writing-functions#multiple-context-stores
Link is just below Using Context in a Flow.
https://nodered.org/docs/user-guide/context#using-context-in-a-flow

This solved it - the section "file" was missing ... thanks a lot!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.