Problem using isNaN()

function only_num(s) {
    for (const character of s) {
        if (isNaN(character)) {
            return false;
        }
    };
    return true;
};
var space_removed = msg.packet.replace(" ", '');
return {
    "packet": msg.packet,
    "space_removed": space_removed,
    "only_num(space_removed)": only_num(space_removed),
};

I used this code inside a function node with a msg.packet "57 029" and this was the output.
It is supposed to be true, not false.
ScreenShot_20230630003947
can anyone explain why ??
I just need to check if a string contains only numbers

I'm using this STRING node it fits most of my requirements ...should fit your ask for trim and return numbers only

1 Like

It works OK for me using your code ?

packet: "57 029"
space_removed: "57029"
only_num(space_removed): true
_msgid: "70af63ef526e09be"

yeah ... and thats how it would look like with the STRING node :wink:

to check if content is numeric only ... see below :wink: very easy

Its a very versatile node I use it myself.

But doesn't help explain why OP code works for OK when I tested it.

@amineDH Here is a modified flow using regex, give this a try

[
    {
        "id": "7f389018cf7f8f27",
        "type": "function",
        "z": "be14baae.1728",
        "name": "function 29",
        "func": "function only_num(space_removed) {\n    return /^\\d+$/.test(space_removed);\n}\n\nlet space_removed = msg.packet.replace(/\\s/g, ''); // Remove white space from packet\n\nreturn {\n    \"packet\": msg.packet,\n    \"space_removed\": space_removed,\n    \"only_num(space_removed)\": only_num(space_removed),\n};\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 790,
        "y": 3480,
        "wires": [
            [
                "f1939185c12e2bd9"
            ]
        ],
        "info": "function only_num(space_removed) {\r\n    return /^\\d+$/.test(space_removed);\r\n}\r\n\r\nlet space_removed = msg.packet.replace(/\\s/g, ''); // Remove white space from packet\r\n\r\nreturn {\r\n    \"packet\": msg.packet,\r\n    \"space_removed\": space_removed,\r\n    \"only_num(space_removed)\": only_num(space_removed),\r\n};\r\n\r\n"
    },
    {
        "id": "a5efb86805716da8",
        "type": "inject",
        "z": "be14baae.1728",
        "name": "",
        "props": [
            {
                "p": "packet",
                "v": "57  029",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 635,
        "y": 3480,
        "wires": [
            [
                "7f389018cf7f8f27"
            ]
        ]
    },
    {
        "id": "f1939185c12e2bd9",
        "type": "debug",
        "z": "be14baae.1728",
        "name": "debug 319",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 955,
        "y": 3480,
        "wires": []
    },
    {
        "id": "ee75475c57de0c19",
        "type": "inject",
        "z": "be14baae.1728",
        "name": "",
        "props": [
            {
                "p": "packet",
                "v": "A 57  029",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 635,
        "y": 3420,
        "wires": [
            [
                "7f389018cf7f8f27"
            ]
        ]
    }
]
1 Like

Thank you very much,
but I've tried this solution and Nexus's solution and still the same problem.

I tested the string node with isNumeric() method.

ScreenShot_20230701161837

The first result is from the "lora-packet" and the second is from the inject node, same packet but different result.
This is my lora-packet function node, is there any problem with it ???

const lora_packet = loraPacket;
try {
    var originalPacket = msg.payload.col16;
    var modifiedPacket = originalPacket.replace(/-/g, "");

    const packet = lora_packet.fromWire(Buffer.from(modifiedPacket, "hex"));
    const NwkSKey = Buffer.from("0D8063CDAEE6450073A7BB93541D068D", "hex");
    const AppSKey = Buffer.from("1B3EF833E8DA2D2845B9E3A8CCDA4ABC", "hex");

    //console.log("Decrypted (ASCII)='" + lora_packet.decrypt(packet, AppSKey, NwkSKey).toString() + "'");
    if (packet.DevAddr == "&�F") {
        return {
            "time": msg.payload.col3,
            "packet": lora_packet.decrypt(packet, AppSKey, NwkSKey).toString()
        };
    }
    else {
        return {};
    }
} catch (error) {}

My best guess is that may be something different in the lora packet we cannot see in the debug?

Can you capture the complete message before it goes into your lora function node, then I can test with it.

I'm reading data from a csv file.

ScreenShot_20230701181659

and this is a message example from the CSV convertor

{"filename":"C:\\Users\\dhami\\Desktop\\pktlog_AA555A0000000101_20230623T162117Z.csv",
"payload":{
"col1":"AA555A0000000101",
"col3":"2023-06-23 16:58:11.202Z",
"col4":1522195652,
"col5":868300000,
"col6":1,"col7":1,
"col8":"CRC_BAD",
"col9":21,
"col10":"LORA",
"col11":125000,
"col12":"SF12  ",
"col13":"05-Apr",
"col14":-21,
"col15":8,
"col16":"C046DA0B-33891A03-1507FE33-AD04F717-59E138C8-B3"},
"parts":{"index":126,"ch":"\n","type":"string","id":"d9ec479b5f811bbb","count":null},
"_msgid":"3157f6e045e3a6b6",
"columns":"col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12,col13,col14,col15,col16"}

Can you please post again using this guide so people can copy paste.

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

1 Like

thanks, pretty helpful .
i hope the my edit fixes it

I'm away at the moment so cannot test this.

Logically same input = same output.
As this is not the case, it cannot be the same input, even though it looks like it in debug.

Can you try writing the output from "lora-packet" to a file and open with notepad++ and look for any odd none printable characters ?

1 Like

Adding 0x00 to the string would fail the function. And probably any other non-printable character would too.

[{"id":"5f8f404c8f069d15","type":"inject","z":"d2d96229134f29f0","name":"","props":[{"p":"packet","v":"57 029","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":770,"y":140,"wires":[["543cf8fd9d4bf86e"]]},{"id":"98aa909eef4126e1","type":"debug","z":"d2d96229134f29f0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1270,"y":140,"wires":[]},{"id":"70d5cc0991a558fa","type":"function","z":"d2d96229134f29f0","name":"","func":"function only_num(s) {\n    for (const character of s) {\n        if (isNaN(character)) {\n            return false;\n        }\n    };\n    return true;\n};\nvar space_removed = msg.packet.replace(\" \", '');\nreturn {\n    \"packet\": msg.packet,\n    \"space_removed\": space_removed,\n    \"only_num(space_removed)\": only_num(space_removed),\n};\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1100,"y":140,"wires":[["98aa909eef4126e1"]]},{"id":"543cf8fd9d4bf86e","type":"function","z":"d2d96229134f29f0","name":"add 0x00","func":"msg.packet = msg.packet + String.fromCharCode(0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":920,"y":140,"wires":[["70d5cc0991a558fa"]]},{"id":"18f2869a5b932066","type":"inject","z":"d2d96229134f29f0","name":"","props":[{"p":"packet","v":"57 029","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":770,"y":100,"wires":[["70d5cc0991a558fa"]]}]
1 Like

I've tried printing it to a file, and eventually I found out that there are null characters attached to the string.

{"col1":"54 029\u0000\u0000"}
{"col1":"55 027\u0000\u0000"}
{"col1":"56 029\u0000\u0000"}

So I just used this to fix it, and my project works fine now.

.replace(/\u0000/g, '');

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