Unwanted nested array

Hi, I'm quite new to Java script and Node Red so looking for help with a hopefully simple but strange issue.

I have a function that create an array with 10 values that I store as a flow variable.
If I check the array_name.length I get 10 in return as expected.

But if I call up the variable in another function it becomes a nested array and array_name.length returns the value 1.

How can I call the array so that it is not nested?

Thanks

[{"id":"20bb0f7f9fab47a8","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"de7faf407b15924b","type":"function","z":"20bb0f7f9fab47a8","name":"","func":"var holidays = flow.get([\"holidays\"]) || [];\nvar x = 0\nfor (i=0; i<msg.payload.length; i++){\n    if (msg.payload[i].type == \"public\") { \n        holidays[x] = msg.payload[i].date.substring(0 , 10)   \n        flow.set([\"holidays\"], [holidays]) \n        x=x+1\n    }\n}\n\n//change format to dd-mm-yyyy\nfor (i=0; i<holidays.length; i++){\n    holidays[i] = holidays[i].substring(8 , 10) + '-' + holidays[i].substring(5 , 7)+ '-' + holidays[i].substring(0 , 4)\n    flow.set([\"holidays\"], [holidays]) \n}\n\nmsg.topic = \"Holidays\"\nmsg.payload = holidays\nnode.warn(holidays)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":180,"wires":[[]]},{"id":"c900d44f65722543","type":"http request","z":"20bb0f7f9fab47a8","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":350,"y":180,"wires":[["de7faf407b15924b"]]},{"id":"b4c1115243f267a4","type":"inject","z":"20bb0f7f9fab47a8","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":100,"wires":[["828ebac7791878b5"]]},{"id":"828ebac7791878b5","type":"function","z":"20bb0f7f9fab47a8","name":"Next day URL","func":"var nowMS = Date.now();\nvar nowDate = new Date(nowMS);\nvar thisyear = nowDate.getFullYear() \nvar holidays = flow.get([\"holidays\"]) || [];\nnode.warn(holidays)\n\n\nh1 = \"https://api.generadordni.es/v2/holidays/holidays?country=ES&year=\"\nmsg.url = h1 + thisyear \n\n// check if holiday array is already created\nif (holidays.length>2){\n    msg.payload = true\n} else {\n    msg.payload = true\n}    \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":100,"wires":[["c900d44f65722543"]]}]

I am not entirely sure how your function node functions, I assume it overwrites the holidays every time ?

Perhaps this flow could help ?

[{"id":"de7faf407b15924b","type":"function","z":"20bb0f7f9fab47a8","name":"set flow","func":"const i = msg.payload\nlet holidays = [] \n\ni.filter(p => p.type==\"public\").forEach(e=>{\n    let d = e.date.split(\" \")[0].split(\"-\")\n    holidays.push(`${d[2]}-${d[1]}-${d[0]}`)\n})\n\nflow.set([\"holidays\"], [holidays]) \nmsg.payload = holidays\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":100,"wires":[["7bc3944f5a63d4f7"]]},{"id":"c900d44f65722543","type":"http request","z":"20bb0f7f9fab47a8","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":430,"y":100,"wires":[["de7faf407b15924b"]]},{"id":"b4c1115243f267a4","type":"inject","z":"20bb0f7f9fab47a8","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":100,"wires":[["828ebac7791878b5"]]},{"id":"828ebac7791878b5","type":"function","z":"20bb0f7f9fab47a8","name":"url","func":"msg.url = `https://api.generadordni.es/v2/holidays/holidays?country=ES&year=${new Date().getFullYear()}`\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":100,"wires":[["c900d44f65722543"]]},{"id":"7bc3944f5a63d4f7","type":"debug","z":"20bb0f7f9fab47a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":100,"wires":[]},{"id":"de7bc6d503a31cb4","type":"inject","z":"20bb0f7f9fab47a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"holidays","payloadType":"flow","x":190,"y":180,"wires":[["518fa3f903f7738a"]]},{"id":"518fa3f903f7738a","type":"debug","z":"20bb0f7f9fab47a8","name":"flow.get holidays","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":390,"y":180,"wires":[]},{"id":"b6fde14c0d86419f","type":"inject","z":"20bb0f7f9fab47a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":260,"wires":[["77c207edc4862278"]]},{"id":"77c207edc4862278","type":"function","z":"20bb0f7f9fab47a8","name":"holiday today ?","func":"const today = new Date().toISOString().split(\"T\")[0].split(\"-\")\nconst d = `${today[2]}-${today[1]}-${today[0]}`\nconst holidays = flow.get(\"holidays\")\nconst checkHoliday = holidays.filter(h => h == d)\nconst payload = (checkHoliday.length > 0 ) ? true : false\n\nmsg.payload = payload\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":260,"wires":[["97045299f5a4f041"]]},{"id":"97045299f5a4f041","type":"debug","z":"20bb0f7f9fab47a8","name":"holiday today ?","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":540,"y":260,"wires":[]}]

If have added a test if today is holiday that could be used if needed.

Remove the square brackets in the flow.get call.

Thanks, I guessed it was something simple. Got a lot to learn, line by line.

Thanks, very clear and it give me ideas how to split up code in Node Red.

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