Function-node freezes whole node-red

Hi

I am currently working on a way to make a hex buffer out of various hex stings.
A microcontroller (ESP32) will be using those hex values as values to compare an input with.
I want to keep the message over MQTT as small as possible. Eventually the size will be 50KB.
The following flow consists of:
-A working function where a few hexstrings will be converted to an integer.
-A not working function where node-red freezes completely... (Use at own risk).

I do not get why this makes it crash...

[{"id":"1b78ffdb.adbf5","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"1cfe95.33e3216c","type":"inject","z":"1b78ffdb.adbf5","name":"FREEZE","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"package\":{\"allowedTokens\":[\"ffffffff\",\"feffffff\",\"fdffffff\",\"f0ffff\",\"ffffffff\",\"0fffffff\",\"ffff\"]}}","payloadType":"json","x":120,"y":340,"wires":[["ea6f02c.7752b"]]},{"id":"ea6f02c.7752b","type":"function","z":"1b78ffdb.adbf5","name":"","func":"var arrayIndex = [];\nvar arrayDB = [];\n\nfor(var i = 0; i<msg.payload.package.allowedTokens.length; i++){\n    \n    if (msg.payload.package.allowedTokens[i].length % 2) {\n        msg.payload.package.allowedTokens[i] = '0' + msg.payload.package.allowedTokens[i];\n    }\n    var index = 0;\n    for(var j = 0; j<msg.payload.package.allowedTokens[i].length; j+2){\n        var subStr = msg.payload.package.allowedTokens[i].substring(j, j + 2);\n        arrayDB[j] = parseInt(subStr,16);\n        index++;\n    }\n    arrayIndex[i] = index;\n            \n}\n//     return bufferAllowedTokens;\n// }\nmsg.payload=\"\";\nmsg.payload.request = {\"request\": \"Test\"};\nmsg.payload.package = {\"allowedTokens\": arrayDB};\nmsg.payload.package = {\"arrayIndex\": arrayIndex};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":340,"wires":[["bfff5362.cab5b"]]},{"id":"bfff5362.cab5b","type":"debug","z":"1b78ffdb.adbf5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":340,"wires":[]},{"id":"9af8572.e34dea8","type":"function","z":"1b78ffdb.adbf5","name":"","func":"for(var i = 0; i<msg.payload.package.allowedTokens.length; i++){\n    // while(msg.payload.package.allowedTokens[i].length != 16){\n        \n    // }\n    msg.payload.package.allowedTokens[i] = parseInt(msg.payload.package.allowedTokens[i],16);\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":140,"wires":[["5ab00037.f7b44"]]},{"id":"48b697fe.837ee8","type":"inject","z":"1b78ffdb.adbf5","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"package\":{\"allowedTokens\":[\"049b26870ec9b92c\",\"c704a48b079c8db9\",\"d267e8f255b65fee\",\"66ca6022885a74e1\",\"3dce7df596f567a0\",\"64ab45796b1a83da\",\"dccf0b13c7dd9098\",\"8c3b38588c2ba11b\",\"aaab4873fe0c94df\",\"7ac8a016\",\"d9fe13b4\",\"03350f2a\"]}}","payloadType":"json","x":110,"y":140,"wires":[["9af8572.e34dea8"]]},{"id":"5ab00037.f7b44","type":"debug","z":"1b78ffdb.adbf5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":140,"wires":[]},{"id":"5788ce3b.073eb","type":"comment","z":"1b78ffdb.adbf5","name":"Working without substring","info":"","x":150,"y":80,"wires":[]},{"id":"bd8f92a6.36318","type":"comment","z":"1b78ffdb.adbf5","name":"[FREEZES Node-Red] Not working with substring","info":"","x":220,"y":280,"wires":[]}]

You are likely creating an endless loop. Use node.warn(?) to check your for loop variables are valid and as expected.

Node-Red freezes completely. I can't do anything...

You can start Node-RED in safe mode by adding the --safe argument to the node-red command.

This will start Node-RED without running your flows. You can then edit the function node to remove the loop and deploy the change to start it running

Yes, but I manually trigger the function. So I can do node-red-stop and node-red-start and it runs again.
But I can't figure out why Node-Red freezes when it runs that function.

I'm not near my laptop to import the whole flow. Can you share just the function code so we can look at it directly?

It seems JSfiddle has also a hard time running this piece...

var arrayIndex = [];
var arrayDB = [];

for(var i = 0; i<msg.payload.package.allowedTokens.length; i++){
    
    if (msg.payload.package.allowedTokens[i].length % 2) {
        msg.payload.package.allowedTokens[i] = '0' + msg.payload.package.allowedTokens[i];
        node.warn(msg.payload.package.allowedTokens[i]);
        
    }
    var index = 0;
    for(var j = 0; j<msg.payload.package.allowedTokens[i].length; j+2){
        var subStr = msg.payload.package.allowedTokens[i].substring(j, j + 2);
        node.warn(subStr);
        arrayDB[j] = parseInt(subStr,16);
        node.warn(arrayDB[j]);
        index++;
    }
    arrayIndex[i] = index;
            
}

msg.payload="";
msg.payload.request = {"request": "Test"};
msg.payload.package = {"allowedTokens": arrayDB};
msg.payload.package = {"arrayIndex": arrayIndex};
return msg;

j+2 should be j+=2

Otherwise you're not incrementing j at all and it'll get stuck in that for loop forever.

:man_facepalming: :man_facepalming:
I think this means I should go to bed then :sweat_smile:

Thanks for the help!

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