# Access context from custom node editor code (.html file)

**URL:** <https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508>\
**Category:** Developing Nodes\
**Created:** [10 January 2025 12:13 UTC](https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508 "2025-01-10T12:13:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayswekkie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mayswekkie/32/99015_2.png) [@mayswekkie](https://discourse.nodered.org/u/mayswekkie)\
**Post date:** [10 January 2025 12:13 UTC](https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508/1 "2025-01-10T12:13:39Z")

</div>

I want to access the global context from a custom node I'm creating. I want to do this from the editor code (html file) in the onprepare function. I read in ([javascript - Send data on configuration - Stack Overflow](https://stackoverflow.com/a/41567832)) that in order to access the context (runtime) from the editor code (which runs in the browser), I can create a RED api in my js file, and call that api from my editor code.

However I don't know how I can access the context from that API I'm creating. this.context().global is not available there (see below).

```auto
module.exports = function (RED) {
  function ModelConfigurationNode(config) {
    RED.nodes.createNode(this, config);
    this.name = config.name;
    this.models = config.models;
  }
  RED.nodes.registerType("model-configuration", ModelConfigurationNode);

  // api to be able to communicate between editor and runtime to read the global context
  RED.httpAdmin.post(
    "/custompath/models",
    RED.auth.needsPermission("model-configuration.read"),
    function (req, res) {
      // read body of the request
      var body = req.body;
      // ACCESS CONTEXT ????
    }
  );
};

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 January 2025 21:03 UTC](https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508/2 "2025-01-10T21:03:07Z")

</div>

Have a look at the uib-cache node in uibuilder:

> <https://github.com/TotallyInformation/node-red-contrib-uibuilder/blob/main/nodes/uib-cache/uib-cache.js#L236-L251>

I pass the store type in from the Editor. `this.storeContext` could be "context", "flow" or "global".

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/14a548e40a1788a782256cdd8cdf495e9170e5c3.png)

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [10 January 2025 21:38 UTC](https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508/3 "2025-01-10T21:38:30Z")

</div>

Julian, he is trying to access the context outside the node.

Get your node (`RED.nodes.getNode(id)`) and then access the context (`node.context()`).

Or read the context directly in the editor - no need your own endpoint.

---

<div class="post-metadata">

**Author:** ![mayswekkie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mayswekkie/32/99015_2.png) [@mayswekkie](https://discourse.nodered.org/u/mayswekkie)\
**Post date:** [13 January 2025 09:08 UTC](https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508/6 "2025-01-13T09:08:00Z")

</div>

Thank you for your answer.  
Can you provide some more information?  
My requirement is to write some data to the global context whenever a user saves some properties in the edit dialog of my custom node.  
How can I read the context from the editor? Can I also write to it?

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [13 January 2025 09:21 UTC](https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508/7 "2025-01-13T09:21:24Z")

</div>

You can get the value from the editor like this:

```js
$.getJSON("context/global", function(data) {
  console.log(data);
});

```

But you cannot write.  
Since you want to write, then you will have to use your endpoint:

```js
RED.httpAdmin.post(
    "/custompath/models/:id",
    RED.auth.needsPermission("model-configuration.read"),
    function (req, res) {
      // read body of the request
      const body = req.body;
      const id = req.params.id
      // ACCESS CONTEXT ????
      const node = RED.nodes.getNode(id);
      if (node) {
        const context = node.context();
      }
    }
  );

```

---

<div class="post-metadata">

**Author:** ![mayswekkie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mayswekkie/32/99015_2.png) [@mayswekkie](https://discourse.nodered.org/u/mayswekkie)\
**Post date:** [13 January 2025 11:12 UTC](https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508/8 "2025-01-13T11:12:22Z")

</div>

I tried your code but it never finds the node even though the id is correctly sent from the editor:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/df2e14420530b372ce5b51302cc689525e301a7b.png)

```auto
function (req, res) {
      // read body of the request
      var body = req.body;
      const id = req.params.id;
      const node = RED.nodes.getNode(id);
      if (node) {
        res.sendStatus(200);
      }
      else {
        res.sendStatus(404); // always get a 404, since node is not found
      }
    }

```

I also tried getting the context through below code, but this also does not work.

```auto
RED.nodes.getContext('global') // TypeError: RED.nodes.getContext is not a function

```

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [13 January 2025 11:23 UTC](https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508/9 "2025-01-13T11:23:55Z")

</div>

The id comes from a deployed node? If the node only exists in the editor, this is normal.

`RED.nodes.getContext` is not exported.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [13 January 2025 11:42 UTC](https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508/10 "2025-01-13T11:42:50Z")

</div>

Well... since you are only looking for the global context you can simplify it like this:

```js
let globalContext;
RED.httpAdmin.post(
  "/custompath/models",
  RED.auth.needsPermission("model-configuration.read"),
  function (req, res) {
    const body = req.body;

    if (!globalContext) {
        RED.nodes.eachNode((node) =>
          globalContext = node.context().global;
          return false; // Not sure but should breaks the loop
        );
    } else {
      // Write your code here
    }
  }
);

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 March 2025 11:43 UTC](https://discourse.nodered.org/t/access-context-from-custom-node-editor-code-html-file/94508/11 "2025-03-14T11:43:20Z")

</div>

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