Loop an array and pass individual elements to next node and come back

I have an API call which returns x serials. I want to take each serial and feed it to the next API call to fetch more data. in short i'm trying to accomplish something like this

arr = [serial1, serial2, serial3...]
for( i=0, i<len(arr), i++)
data = request.get(url, arr[i])
#do something with data here and move on to the next i

I used a split node to get all my serial numbers in array form, but only the first serial number is passed on to the next node. my function node is as follows

msg.payload.forEach(d => {
    serial = d.id;
    msg.payload = serial;
    node.send(msg);
})

I'm have just started learning node-red, any direction is much appreciated

Here is a demo to get you moving...

Demo flow (Import it into your node-red using CTRL+I)

[{"id":"6bd960618adeaf79","type":"http in","z":"8ff35f2f8018f9c7","name":"","url":"/api1","method":"get","upload":false,"swaggerDoc":"","x":1640,"y":200,"wires":[["4d712c45698356c2"]]},{"id":"69b5c69aee5912f7","type":"http response","z":"8ff35f2f8018f9c7","name":"","statusCode":"","headers":{},"x":1990,"y":200,"wires":[]},{"id":"0fb1db23201d87c3","type":"http in","z":"8ff35f2f8018f9c7","name":"","url":"/api2/:serial","method":"get","upload":false,"swaggerDoc":"","x":1660,"y":240,"wires":[["fadb3847b1eb9bc0"]]},{"id":"a1711f3d2d9e892d","type":"http response","z":"8ff35f2f8018f9c7","name":"","statusCode":"","headers":{},"x":1990,"y":240,"wires":[]},{"id":"4d712c45698356c2","type":"function","z":"8ff35f2f8018f9c7","name":"data","func":"msg.payload = [\n    \"abc123\",\n    \"def321\",\n    \"xyz987\",\n]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1830,"y":200,"wires":[["69b5c69aee5912f7"]]},{"id":"138c685c77c555c8","type":"http request","z":"8ff35f2f8018f9c7","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://localhost:1880/api1","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1810,"y":380,"wires":[["5f62c1722e2fe05d"]]},{"id":"772e9b2431953e82","type":"inject","z":"8ff35f2f8018f9c7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1660,"y":380,"wires":[["138c685c77c555c8"]]},{"id":"5f62c1722e2fe05d","type":"split","z":"8ff35f2f8018f9c7","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1950,"y":380,"wires":[["acfa01919965eee6","0ccc23004fc92386"]]},{"id":"acfa01919965eee6","type":"http request","z":"8ff35f2f8018f9c7","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://localhost:1880/api2/{{payload}}","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1670,"y":460,"wires":[["f2e1c4c32a4951eb"]]},{"id":"f2e1c4c32a4951eb","type":"join","z":"8ff35f2f8018f9c7","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":1840,"y":460,"wires":[["9cae35c0b9d3cc56"]]},{"id":"fadb3847b1eb9bc0","type":"function","z":"8ff35f2f8018f9c7","name":"data","func":"\nlet serial = msg.req.params.serial;\nmsg.payload = {\n    serial : serial,\n    serialB64: Buffer.from(serial).toString(\"base64\"),\n    \"info\": \"Created by API2\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1830,"y":240,"wires":[["a1711f3d2d9e892d"]]},{"id":"9cae35c0b9d3cc56","type":"debug","z":"8ff35f2f8018f9c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2130,"y":460,"wires":[]},{"id":"a2c0438ca5a1e46f","type":"comment","z":"8ff35f2f8018f9c7","name":"The API (dummy)","info":"","x":1660,"y":160,"wires":[]},{"id":"777b51053f53c18d","type":"comment","z":"8ff35f2f8018f9c7","name":"Call API1, split the results, call 2nd API join the results","info":"","x":1780,"y":340,"wires":[]},{"id":"0ccc23004fc92386","type":"debug","z":"8ff35f2f8018f9c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2130,"y":380,"wires":[]}]

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

1 Like

Thank you! the Split and join method works. Also added some delay after split node to rate limit 1 msg per second.

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