Error while creating my first custom node - "ReferenceError: done is not defined"

Hi! I'm trying to create my first custom node, but I get this weird error: "ReferenceError: done is not defined" when I try to run my custom node. The thing is I don't use "done" method anywhere in the code. The node is supposed to be simple hysteresis controller.
.js file:

module.exports = function (RED) {
  function HysteresisNode(config) {
    RED.nodes.createNode(this, config);
    let node = this;
    node.on("input", function (msg) {
      const highPlus = msg.payload.high;
      const lowPlus = msg.payload.low;
      const threshold = msg.payload.threshold;
      const current = msg.payload.current;

      if (threshold + highPlus >= current) {
        msg.payload = node.highOutput;
        node.send(msg);
      } else if (threshold - lowPlus <= current) {
        msg.payload = node.lowOutput;
        node.send(msg);
      }
    });
  }

  RED.nodes.registerType("hysteresis", HysteresisNode);
};

.html file:

<script type="text/javascript">
  RED.nodes.registerType("hysteresis", {
    label: "hysteresis",
    category: "function",
    color: "#a6bbcf",
    inputs: 1,
    outputs: 1,
    icon: "font-awesome/fa-cogs",
    defaults: {
      highOutput: { value: false },
      lowOutput: { value: false },
      highOutputType: { value: "bool" },
      lowOutputType: { value: "bool" },
    },
    oneditprepare: function () {
      $("#node-input-highOutput").typedInput({
        type: "bool",
        types: ["bool", "num", "str"],
        default: "bool",
        typeField: "#node-input-highOutput-type",
      });

      $("#node-input-lowOutput").typedInput({
        type: "bool",
        types: ["bool", "num", "str"],
        default: "bool",
        typeField: "#node-input-lowOutput-type",
      });
    },
  });
</script>

<script type="text/html" data-template-name="hysteresis">
  <p>Output values:</p>
  <div class="form-row">
    <label for="node-input-highOutput"
      ><i class="fa fa-tag"></i>Upper Threshold</label
    >
    <input type="text" id="node-input-highOutput" />
    <input type="hidden" id="node-input-highOutputType" />
  </div>
  <div class="form-row">
    <label for="node-input-lowOutput"
      ><i class="fa fa-tag"></i>Lower Threshold</label
    >
    <input type="text" id="node-input-lowOutput" />
    <input type="hidden" id="node-input-lowOutputType" />
  </div>
  <div class="form-tips"><b>Tip:</b> This is here to help.v</div>
</script>

package.json

{
  "name": "node-red-contrib-easy-hysteresis",
  "version": "1.0.3",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "node-red": {
    "nodes": {
      "hysteresis": "hysteresis.js"
    }
  }
}

As the documentation says:

With Node-RED 1.0, a new style of the listener was introduced to allow the node to notify the runtime when it has finished handling a message. This added two new parameters to the listener function. Some care is needed to ensure the node can still be installed in Node-RED 0.x which does not use this new style of the listener.

this.on('input', function(msg, send, done) {

Did you check if the error persists if you use the correct / current interface definition of

[...].on('input', function(msg, send, done) { ... }

I've changed my code according to documentation, yet, the same error occurs.

.js:

module.exports = function (RED) {
  function HysteresisNode(config) {
    RED.nodes.createNode(this, config);
    let node = this;
    node.on("input", function (msg, send, done) {
      const highPlus = msg.payload.high;
      const lowPlus = msg.payload.low;
      const threshold = msg.payload.threshold;
      const current = msg.payload.current;

      send =
        send ||
        function () {
          node.send.apply(node, arguments);
        };

      if (threshold + highPlus >= current) {
        msg.payload = node.highOutput;
        send(msg);
      } else if (threshold - lowPlus <= current) {
        msg.payload = node.lowOutput;
        send(msg);
      }

      if (done) {
        done();
      }
    });
  }

  RED.nodes.registerType("hysteresis", HysteresisNode);
};

ReferenceError: done is not defined

The code you posted looks fine. Are you sure, this error is coming from your custom node?
If you share a link to a repository or post the content of both files here, I could try to test your node on my local system - for verification purposes.

I'm sorry I haven't responded for so long.
Here's the .tgz archive with all the files (HTML and JSON files haven't been changed since my first post): https://we.tl/t-YUvS5APLPc.
And this is the flow I'm testing my node at:

[
    {
        "id": "5fc79c843f20daa3",
        "type": "function",
        "z": "02ac266ad9de5972",
        "name": "function 15",
        "func": "msg.payload = {\n    high: 0.2,\n    low: 0.3,\n    threshold: 25,\n    current: 24.8\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 450,
        "y": 300,
        "wires": [
            [
                "24f94a71fc033aa1"
            ]
        ]
    },
    {
        "id": "10dc9f46fc794db2",
        "type": "debug",
        "z": "02ac266ad9de5972",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 740,
        "y": 300,
        "wires": []
    },
    {
        "id": "b5f837479d3a02c4",
        "type": "inject",
        "z": "02ac266ad9de5972",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 300,
        "y": 300,
        "wires": [
            [
                "5fc79c843f20daa3"
            ]
        ]
    },
    {
        "id": "24f94a71fc033aa1",
        "type": "hysteresis",
        "z": "02ac266ad9de5972",
        "highOutput": "false",
        "lowOutput": "false",
        "highOutputType": "bool",
        "lowOutputType": "bool",
        "x": 600,
        "y": 300,
        "wires": [
            [
                "10dc9f46fc794db2"
            ]
        ]
    }
]

Whereas your node (currently) isn't doing anything useful, it's not creating the error you see either - on my system:

To stay fair: The node does what it is told to do. highOutput and lowOutput are undefined. Just follow this section in the documentation to manage the TypedInput control correctly.

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