Receiving "RangeError: Maximum call stack size exceeded" from function node

Hello Node-Red community! I'm having a bit of trouble with a flow I'm making for a Discord Bot that takes a message, parses it, then outputs each number to its own text file. I have each half working independently. If I disconnect the file node, it will output the desired payload into debug. Then, if I reattach the file node, and disconnect the discord message node, I can inject a typical message and it will output the files as needed. The issue seems to occur when I have both attached. I keep getting the error: "RangeError: Maximum call stack size exceeded" when I attempt to parse the discord messages then output to file. It seems to be the second function node that throws the error. Is there an inefficiency in the way I've written that second function? I've tried the delay/message limit node in a few locations, that didn't make any difference. Am I missing something simple?

[{"id":"f653e254f7e1139a","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"989c7578cc7ab40c","type":"file","z":"f653e254f7e1139a","name":"","filename":"","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":880,"y":60,"wires":[[]]},{"id":"a2b7bb90be583841","type":"function","z":"f653e254f7e1139a","name":"","func":"msg.payload=msg.payload.split(\":\");\nmsg.payload=msg.payload[1].split(\"Result\");\nmsg.payload=msg.payload[0].split(\",\"); \nconst regex = /\\D/;\n\nfor (var i = 0; i < msg.payload.length; i++){\n    for(var e = 0; e < msg.payload[i].length+2;e++){\n        msg.payload[i] = msg.payload[i].replace(regex, \"\")\n    }\n    msg.payload[i]=msg.payload[i].replace(\" \",\"\");\n}\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":60,"wires":[["ae59b41a057b3d3f"]]},{"id":"ae59b41a057b3d3f","type":"function","z":"f653e254f7e1139a","name":"","func":"convertToDice = msg.payload\nmsg.filename = \"\"\nfor (i = 0; i < convertToDice.length; i++){\n    diceNum = \"dice\" + (i+1)\n    msg.filename = \"/home/pi/Documents/\" + diceNum + \".txt\";\n    msg.payload = convertToDice[i];\n    node.send(msg);\n    node.done();\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":60,"wires":[["e8823236e6e79e37","989c7578cc7ab40c"]]},{"id":"a1f4910557681a62","type":"inject","z":"f653e254f7e1139a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Jordacai Roll: [6, 5, 3, 2, 3, 5] Result: 16","payloadType":"str","x":330,"y":100,"wires":[["a2b7bb90be583841"]]},{"id":"e8823236e6e79e37","type":"debug","z":"f653e254f7e1139a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":100,"wires":[]},{"id":"9140f1140b3c4efa","type":"discord-get-messages","z":"f653e254f7e1139a","name":"","token":"","channels":"#roll-dice","x":150,"y":60,"wires":[["ea7d2c6a6eaa089b"]]},{"id":"ea7d2c6a6eaa089b","type":"switch","z":"f653e254f7e1139a","name":"","property":"member.user.username","propertyType":"msg","rules":[{"t":"regex","v":"Dice Maiden","vt":"str","case":false}],"checkall":"true","repair":false,"outputs":1,"x":330,"y":60,"wires":[["a2b7bb90be583841"]]}]

The call to node.done right after the node.send may cause issues, and best to return null at end. Also try rate limiting before the file node using a delay node set to rate limit.

Have a look at this example

[{"id":"a1f4910557681a62","type":"inject","z":"f653e254f7e1139a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Jordacai Roll: [6, 5, 3, 2, 3, 5] Result: 16","payloadType":"str","x":170,"y":80,"wires":[["a2b7bb90be583841"]]},{"id":"a2b7bb90be583841","type":"function","z":"f653e254f7e1139a","name":"","func":"msg.payload = msg.payload.split(/\\[|]/);\nlet convertToDice = msg.payload[1].split(\",\").map(e => e.trim()); \nfor (let i = 0; i < convertToDice.length; i++){\n    msg.filename = `/home/pi/Documents/dice${i+1}.txt`;\n    msg.payload = convertToDice[i];\n    node.send(msg);\n}\nnode.done();\nreturn null","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":80,"wires":[["e8823236e6e79e37"]]},{"id":"e8823236e6e79e37","type":"debug","z":"f653e254f7e1139a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":80,"wires":[]}]

That is a much more efficient way to write that function! Unfortunately, it returned the same error. It will still output into the debug node and files if injected, but not if received from payload from Discord node. Discord Node will still parse correctly if file node is disconnected. I also noticed that the rate limit didn't seem to delay the messages at all when I injected the payload.

[{"id":"f653e254f7e1139a","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"989c7578cc7ab40c","type":"file","z":"f653e254f7e1139a","name":"","filename":"","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":780,"y":60,"wires":[[]]},{"id":"a1f4910557681a62","type":"inject","z":"f653e254f7e1139a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Jordacai Roll: [6, 5, 3, 2, 3, 5] Result: 16","payloadType":"str","x":290,"y":100,"wires":[["1d5fa17768faa0ad"]]},{"id":"e8823236e6e79e37","type":"debug","z":"f653e254f7e1139a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":100,"wires":[]},{"id":"9140f1140b3c4efa","type":"discord-get-messages","z":"f653e254f7e1139a","name":"","token":"","channels":"#roll-dice","x":130,"y":60,"wires":[["ea7d2c6a6eaa089b"]]},{"id":"ea7d2c6a6eaa089b","type":"switch","z":"f653e254f7e1139a","name":"","property":"member.user.username","propertyType":"msg","rules":[{"t":"regex","v":"Dice Maiden","vt":"str","case":false}],"checkall":"true","repair":false,"outputs":1,"x":290,"y":60,"wires":[["1d5fa17768faa0ad"]]},{"id":"1d5fa17768faa0ad","type":"function","z":"f653e254f7e1139a","name":"","func":"msg.payload = msg.payload.split(/\\[|]/);\nlet convertToDice = msg.payload[1].split(\", \").map(e => e.trim()); \nfor (let i = 0; i < convertToDice.length; i++){\n    let diceNum = \"dice\" + (i+1)\n    msg.filename = `/home/pi/Documents/${diceNum}.txt`;\n    msg.payload = convertToDice[i];\n    node.send(msg);\n}\nnode.done();\nreturn null","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":60,"wires":[["e8823236e6e79e37","cca1a704cd358f59"]]},{"id":"cca1a704cd358f59","type":"delay","z":"f653e254f7e1139a","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":610,"y":60,"wires":[["989c7578cc7ab40c"]]}]

Can you copy the full msg object from discord node and paste here.

Two objects are returned. One from the user 'rolling the dice' and one from the Pre-existing dicebot (which is the message that we are parsing.) I've got it running through that switch node to filter out all non-dice-bot messages. The Objects are quite large, so they exceed the character limit of the post when put in the same post. I'll have to post them individually.

Are there other msg properties other than payload and are they large?
try clearing the msg object in the function maybe.

[{"id":"1d5fa17768faa0ad","type":"function","z":"c791cbc0.84f648","name":"","func":"msg.payload = msg.payload.split(/\\[|]/);\nlet convertToDice = msg.payload[1].split(\", \").map(e => e.trim()); \nmsg ={};\nfor (let i = 0; i < convertToDice.length; i++){\n    let diceNum = \"dice\" + (i+1)\n    msg.filename = `/home/pi/Documents/${diceNum}.txt`;\n    msg.payload = convertToDice[i];\n    node.send(msg);\n}\nnode.done();\nreturn null","outputs":1,"noerr":0,"initialize":"","finalize":"","x":470,"y":800,"wires":[["e8823236e6e79e37","cca1a704cd358f59"]]}]

Objects are too large to paste into comment, even individually. Tried your method of clearing the object didn't seem to work at first. After I removed the rate-limit node, it seems to work fine now. Thank you so much for your help! I didn't think about clearing the whole message object. Something so simple!

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