Problem with context

I'm running node-red under Docker. I'm trying to use the filesystem for my global context. My node-red volume is mounted to :/data:Z

I've set my settings.js as...

contextStorage: {
      storeInFile: {
          module: "localfilesystem",
           config: {
               dir: "/data/.node-red",
               base: "context",
               cache: false
           }
       },
      default: { module: "memory" }
   },

If I issue...

global.set("test", "Hello world", "storeInFile");

... I am unable to retrieve this value using global.get("test");

If I bash to my docker instance, I see a folder /context under /data (not under /.node-red). If I browse to /data/context/global and cat global.json I see...

{
     "test": "Hello world"
}

What gives? What am I doing wrong? It acts like it's writing to one path /data/context/global but trying to read from another /data/.node-red/context/global.

How exactly are you trying to get the value?

Because you have set cache: false in your config, the store is in asynchronous access mode.

That means in a function node, you'd have to do:

global.get("test", "storeInFile", function(val) {
   // do something with `val`
});

Edit: Also, in your post you are doing the get without the store name - which means it will be going to your default store, not the file store.

Thanks, that sorted me out. I didn't see any reference to the overload for global.get() that took the store name. I had tried putting the name in there but it threw a callback error, which now makes sense knowing that cache: false is async.

I completely missing this line in the docs, "If the caching mode is disabled, the store only supports asynchronous access."

1 Like