Export data from a loop
Pipedream makes it easy to build event-driven workflows with managed auth (just connect your account and reference tokens and keys in code!). Data is passed between steps in a workflow using step exports, which can be inspected and referenced by later steps. In this post, I'll explain how to export data from a loop:
There are two ways to export data from a loop in a Node.js code step on Pipedream:
- Use a step export array
- Return an array object after the loop is complete
Use a step export array
To export data from a loop using step exports:
- Define a step export as an array
- Add the data you want to export for each loop execution to that array
- Export the array
this.testExport = []
for (let i=0; i < 5; i++) {
this.testExport.push(i)
}
When your workflow executes, the step will export an array at steps.STEP_NAME.testExport
with a length of 5
and values from 0
- 4
.
Copy and run this workflow to test it out. On each loop execution, the code makes an Axios request to the Star Wars API, and it assigns the response data to steps.example.testExport
:
https://pipedream.com/@pravin/export-axios-response-data-from-a-loop-p_G6C1bG/edit
Return an array object after the loop is complete
To return an array after the loop is complete:
- Define an array
- Add the data you want to export for each loop execution to that array
- Return the array
data = []
for (let i=0; i < 5; i++) {
data.push(i)
}
return i
When your workflow executes, the step will export an array at steps.STEP_NAME.$return_value
with a length of 5
and values from 0
- 4
.
Copy and run this workflow to test it out. On each loop execution, the code makes an Axios request to the Star Wars API, and it assigns the response data to steps.example.$return_value
:
https://pipedream.com/@pravin/return-axios-response-data-from-a-loop-p_NMC2Yy/edit