How to repeat a flow based on an array of objetcts

Hi, I'm new to node red and i got this project where a need to repeat a flow that does 3 http requests and save information from then.

in the moment my flow is this one

in the moment i have this on my template node:

image

I'm using it to change the parameters on my "set data" node and it works fine for just one iten in the array of the template. But i need that this repeats from the 3 itens in the array: code 1, code 2 and code 3.

I've tried using the split node but idk how to save the code in a way that can repeat the request and save the infos i need from there.

Is there any node that could help me solve that? i'm kinda lost in it and if anyone have a link that could help me i woud appreciate it.

Idk if its clear what i intend to do, my english its not the best, im sorry for that.

edit: thats my "set data" node at the moment:

image

I guess tell us what the end result is that you are trying to achieve ??

Just guessing but i assume you are wanting to make 3 seperate requests and have the results all come back as one ?

If so do each requst individually, take the result and use a change node on each one to move the msg.payload to (say) msg.payload.req1 etc - you can then use the join node or a function node to put them all back into a single message for further procssing

Craig

I need to make 3 requests for each iten in the array and save information from this requests into a flow.set that i already have in my flow.

is that clear?

So each request is completely independent of the other requests - it is just the results that you wish to combine together ?

Start off small

Make a single request using the HTTP node and put a debug node on the return , then w can start building it up from there.

Also please read at the top of the forum how to post your flows correctly so others can download and install them to assist you

Craig

Yes, each request is completely independent, i'm using just one request here to demonstrate what i need

I'm sending the flow

flows(6).json (2.0 KB)

This is working for just one iten in the array of the template, what i need its a way to repeat it to the entire array

Just split the array then move msg.payload.code to msg.linhas.

e.g.

[{"id":"682b754e.bcf20c","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":580,"wires":[["7e8223e5.e883dc"]]},{"id":"7e8223e5.e883dc","type":"template","z":"c74669a0.6a34f8","name":"","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"[\n    {\n        \"code\" : \"1\",\n        \"CodigoUnidade\" : \"803\"\n    },\n    {\n        \"code\" : \"2\",\n        \"CodigoUnidade\" : \"803\"\n    },\n    {\n        \"code\" : \"3\",\n        \"CodigoUnidade\" : \"803\"\n    }\n]","output":"json","x":330,"y":580,"wires":[["dc91b488.aefc48"]]},{"id":"dc91b488.aefc48","type":"split","z":"c74669a0.6a34f8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":470,"y":580,"wires":[["a19706c1.78913"]]},{"id":"a19706c1.78913","type":"change","z":"c74669a0.6a34f8","name":"set data","rules":[{"t":"set","p":"linha","pt":"msg","to":"payload.code","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":660,"wires":[["6f18fa2a.d351a4"]]},{"id":"6f18fa2a.d351a4","type":"http request","z":"c74669a0.6a34f8","name":"req - PL","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://60a1a403d2855b00173b2521.mockapi.io/pl-{{linha}}","tls":"","persist":false,"proxy":"","authType":"","x":600,"y":660,"wires":[["1e069998.b34d16"]]},{"id":"1e069998.b34d16","type":"function","z":"c74669a0.6a34f8","name":"","func":"//flow.set('coletas_dia', msg.payload.payload.content.filter(e => e.dataColeta.startsWith('2021-04-04')))\n\nflow.set('coletas_dia', [])\n\n_coletasDia = flow.get('coletas_dia')\n\nmsg.payload.payload.content.filter(e => e.dataColeta.startsWith('2021-04-04')).forEach(i => _coletasDia.push({\n    valor : i.valor,\n    loteExterno : i.numeroExternoLote\n}))\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":760,"y":660,"wires":[["1b5d819e.15c73e"]]},{"id":"1b5d819e.15c73e","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":910,"y":660,"wires":[]}]
1 Like

that actually works fine but then my function doesn't save the data in my context "coletas_dia", it just saves from the last iten in the array

But i guess i can work from that, thanks a lot for your help!!

save the results to context in array format and push new result to the context

var results = flow.get("var") || [];
var requestresult = create object of results or string etc;
results.push(requestresults);
flow.set("var", results);
etc

something like above

1 Like

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