javascriptnode.jsnode-red

Why is my node-red injection node no longer working after my flow is deployed?


I wanted to create an injection node, which sends "start" or "stop" depending on the user's choice, when I launch node red and I don't touch my flow, if I press the button on my node, the message is transmitted to the debug node, but if I redeploy my flow, I thought I understood with my lines of code to debug that the node.receive no longer had any effect?

Declencher.js:

module.exports = function(RED) {
    function Declencher(config) {
        RED.nodes.createNode(this, config);
        var node = this;

        this.on('input', function(msg) {
            console.log('before send', this.action); 

            node.send(msg);
            console.log('after send');
        });

        RED.httpAdmin.post('/declencher', function(req, res) {
            
            node.action = req.body.action; 
            node.log("action: " + node.action);

            var newMsg = { payload: node.action }; 

            node.log("action : " + newMsg.payload);
            console.log('Message généré et envoyé');

            node.receive(newMsg); 
            console.log('after receive');

            res.status(200).send({ success: true }); 
        });

        this.on('close', function(removed, done) {
            if (removed) {
                node.log("removed");
            } else {
                node.log("redeployed");
            }
            done();
        });
    }

    RED.nodes.registerType("Déclencher", Declencher);
};

Declencher.html:

<script type="text/javascript">
    RED.nodes.registerType('Déclencher', {
        category: 'RCE',
        color: '#9c5cdf',
        defaults: {
            name: { value: "" },
            action: { value: "start" }
        },
        inputs: 1,
        outputs: 1,
        icon: "https://nodered.org/docs/creating-nodes/images/db.svg",
        label: function() {
            return this.name || "Déclencher";
        },
        oneditprepare: function () {
                $("#node-input-choix").val(this.action); 
        },
        oneditsave: function () {
                this.action = $("#node-input-choix").val(); 
        },
        button: {
            enabled: function() {
                return !this.changed;
            },
            onclick: function() {
                console.log('click');
                $.ajax({
                    type: 'POST',
                    url: '/declencher', 
                    contentType: 'application/json', 
                    data: JSON.stringify({
                        action: this.action  
                    }),
                    success: function(response) {
                        console.log('response : ', response);
                    },
                    error: function(xhr, status, error) {
                        console.error('error : ', error);
                    }
                });

            }
        }
});
</script>

<script type="text/html" data-template-name="Déclencher">
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Nom</label>
        <input type="text" id="node-input-name" placeholder="Nom">
    </div>

    <!-- Menu déroulant pour le choix -->
    <div class="form-row">
        <label for="node-input-choix"><i class="fa fa-list"></i> Choix</label>
        <select id="node-input-choix">
            <option value="start">Activer</option>
            <option value="stop">Désactiver</option>
        </select>
    </div>
</script>

This line: node.receive(newMsg); Doesn't work

Does anyone have any idea what's going on? (I can provide more details if needed)

I tried to do node.send() instead of node.receive, without going through the this.on('input') but I'm new to js and nodejs programming so I don't understand everything


Solution

  • You can't have the RED.httpAdmin.post() in the node definition function. This will try and add a new endpoint on the same route for every instance of the node.

    You need to move it outside the function and update it to include the node id to trigger things.

    Looks at the actual inject node code for an example:

    https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/common/20-inject.js