How to use context in custom node

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

https://nodered.org/docs/creating-nodes/context
my node

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 :

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

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

or

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

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

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

yes I solved ths thx

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

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

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

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