I have a requirement where in I need to set flow level variable, when a user edits a parameter of a custom node. I am trying to do it, in "Validate" function of the parameter. But I am unable to access the flow object getting below error:
this.context is not a function
I am trying to access flow variable using below method as mentioned in documentation.
var flowContext = this.context().flow;
Is there any way to accomplish this task? Please help.
The validate function runs in the editor and has no access to context which exists in the runtime.
Why do you need to set a flow context variable? It would be better to set that variable as part of your node's constructor function that is called when the node is actually deployed.
If you try to set values in context whilst the user is editing the node, you then have to worry about the case where the user closes the editor and abandons the change before ever hitting the deploy button - you'd end up with information in context that was no longer needed. This is why (along with the fact the editor cannot access context) it is better to do things like that as part of the deploy.
I can try saving flow variable, in node's constructor. But this variable value is required in all other custom node's constructors. The value defines a unique key. Hence I cannot make sure that in other custom nodes, value will be available, unless the defining node is constructed.
Do you mean in the other instances of your custom node? If so, I think you want a configuration node. This is created in parallel to your main node and allows you to share configuration between instances. There are lots of examples of this. Try looking at the mqtt nodes for example. That consists of 3 nodes - mqtt-in, mqtt-out and an mqtt configuration node.
It is also possible to share data between instances by creating variables in your js file at the right level. uibuilder has some examples of that where variables are deliberately created early so they need only be created once and shared across all instances.
If you are trying to share things between different node packages/modules, you'll find a possible workaround in my node-red-contrib-jktesting nodes - this is only my by GitHub, it isn't published as it is just a set of examples of how to do things in custom nodes.
Yes I am trying to share data between 2 node types. I had a look into your package. Can you please explain me which part of the code I can have a look? I saw that flow level variables has been described in comments. Couldn't see any code for the usage.
Really, you need to do some background reading into how Node.js modules work. This is the key to understanding how you can share resources between nodes.
require in each of your nodes.
A node.js module is only ever loaded once in an application, even though it may be
required in multiple places.
exports a set of resources. However, you can also have module-level data that isn't exported and this is shared between all code that references the module. uibuilder makes extensive use of this so that multiple instances of uibuilder in your flows have only a single set of ExpressJS web endpoints for delivering front-end libraries.
It is somewhat mind-bending to get your head around initially which is why it is better to find a tutorial somewhere and work through it.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.