node-red/node-red-nodes

node-red-node-random reports incorrect error when msg.from or msg.to is 0

phoddie opened this issue · 2 comments

Which node are you reporting an issue on?

The Node-RED random node.

What are the steps to reproduce?

Run the following flow. It will report Random: one of the input values is no a number. To: 0.

[
    {
        "id": "d25c2a7470d28b6a",
        "type": "tab",
        "label": "link test A",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "8d75e4271768756e",
        "type": "random",
        "z": "d25c2a7470d28b6a",
        "name": "",
        "low": "",
        "high": "",
        "inte": "true",
        "property": "random",
        "_mcu": false,
        "x": 380,
        "y": 340,
        "wires": [
            [
                "406d92ba845cf8df"
            ]
        ]
    },
    {
        "id": "5814d9b8b8729624",
        "type": "inject",
        "z": "d25c2a7470d28b6a",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "from",
                "v": "100",
                "vt": "str"
            },
            {
                "p": "to",
                "v": "0",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": false,
        "onceDelay": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "_mcu": false,
        "x": 210,
        "y": 340,
        "wires": [
            [
                "8d75e4271768756e"
            ]
        ]
    },
    {
        "id": "406d92ba845cf8df",
        "type": "debug",
        "z": "d25c2a7470d28b6a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": false,
        "complete": "random",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "_mcu": false,
        "x": 560,
        "y": 340,
        "wires": []
    }
]

What do you expect to happen?

The range is valid. The flow should generate random integers between 0 and 100 (inclusive).

Potential source of bug

This code pattern appears to be the problem:

            } else if ('from' in msg) { // else see if a 'from' is in the msg
                if (Number(msg.from)) { // if it is, and is a number, use it
                    tmp.low = Number(msg.from);
                } else {                // otherwise setup NaN error
                    tmp.low = NaN;
                    tmp.low_e = " From: " + msg.from; // setup to show bad incoming msg.from
                }

Number coerces its argument to a number. If the value cannot be converted to a valid number, the return value is NaN. As written, the check considers 0 to be invalid because, like NaN it evaluates to false. Something like this might worK better and only coerces msg.from once in the success case.:

            } else if ('from' in msg) { // else see if a 'from' is in the msg
                tmp.low = Number(msg.from);
                if (isNaN(tmp.low)) 
                    tmp.low_e = " From: " + msg.from; // setup to show bad incoming msg.from

A similar change would be necessary for tmp.high.

Please tell us about your environment:

  • Node-RED version: v2.2.2
  • node.js version: v18.8.0
  • npm version: 8.18.0
  • Platform/OS: macOS 12.5.1
  • Browser: N/A

Thanks - fixed in 0.4.1

Excellent. Thank you!