# How to use context in custom node

**URL:** <https://discourse.nodered.org/t/how-to-use-context-in-custom-node/53486>\
**Category:** Developing Nodes\
**Created:** [10 November 2021 08:11 UTC](https://discourse.nodered.org/t/how-to-use-context-in-custom-node/53486 "2021-11-10T08:11:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![d0d04m3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/d0d04m3/32/19965_2.png) [@d0d04m3](https://discourse.nodered.org/u/d0d04m3)\
**Post date:** [10 November 2021 08:11 UTC](https://discourse.nodered.org/t/how-to-use-context-in-custom-node/53486/1 "2021-11-10T08:11:38Z")

</div>

so i create new node  
and need to sotre some data in context node

[https://nodered.org/docs/creating-nodes/context](https://nodered.org/docs/creating-nodes/context)  
my node

```auto
module.exports = function(RED) {

    function ClientNode(config) {

        RED.nodes.createNode(this,config);

        var node = this;

     

        context.set("myData",1);

       
           

        node.on('input', function(msg) {

            msg.payload = this.context.get("myData");

            // msg.payload = msg.payload.toLowerCase();

            node.send(msg);

           

        });

        setTimeout(function () {

           // this.context().flow.set("test", 1)

            var msg = context.get("myData");

            // msg.payload = msg.payload.toLowerCase();

            node.send(msg);

        }, 3000);

    }

    RED.nodes.registerType("Client",ClientNode);

    RED.httpAdmin.post("/client/:id", RED.auth.needsPermission("client.write"), function(req,res) {

        var node = RED.nodes.getNode(req.params.id);

        if (node != null) {

            try {

                msg = {payload:"", topic:""}

               

                msg.payload = node.name;

                msg.topic = node.id

                msg.z = node.z

                node.send(msg);

                res.sendStatus(200);

            } catch(err) {

                res.sendStatus(500);

                node.error("Restore failed: "+ err.toString());

            }

        } else {

            res.sendStatus(404);

        }

    });

}

```

but I cant  
am try with :

```auto
this.context().set("data", 1);
this.context.set("data", 1);
context().set("data", 1);
context.set("data", 1);

```

but all not work  
error in console log

```auto
10 Nov 08:54:34 - [error] [Client:728bd51e7616989f] ReferenceError: context is not defined

```

or

```auto
10 Nov 08:53:16 - [error] [Client:728bd51e7616989f] TypeError: this.context.set is not a function

```

---

<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 November 2021 12:24 UTC](https://discourse.nodered.org/t/how-to-use-context-in-custom-node/53486/2 "2021-11-10T12:24:20Z")

</div>

I've not used context in a custom node but have you tried something like:

```auto
const nodeContext = this.context()
nodeContext.set('data', 1)

```

---

<div class="post-metadata">

**Author:** ![d0d04m3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/d0d04m3/32/19965_2.png) [@d0d04m3](https://discourse.nodered.org/u/d0d04m3)\
**Post date:** [11 November 2021 10:45 UTC](https://discourse.nodered.org/t/how-to-use-context-in-custom-node/53486/3 "2021-11-11T10:45:14Z")

</div>

yes I solved ths thx

---

<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:** [11 November 2021 11:50 UTC](https://discourse.nodered.org/t/how-to-use-context-in-custom-node/53486/4 "2021-11-11T11:50:56Z")

</div>

If you wanted a single line rather than an intermediate variable, you might try:

```auto
(this.context()).set('data', 1)

```

Not tried that but I think it would work (note the extra brackets).

---

<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:** [25 November 2021 11:51 UTC](https://discourse.nodered.org/t/how-to-use-context-in-custom-node/53486/5 "2021-11-25T11:51:53Z")

</div>

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