DEV Community

Pipedream
Pipedream

Posted on • Originally published at blog.pipedream.com

1

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:

  1. Use a step export array
  2. Return an array object after the loop is complete

Use a step export array

To export data from a loop using step exports:

  1. Define a step export as an array
  2. Add the data you want to export for each loop execution to that array
  3. 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:

  1. Define an array
  2. Add the data you want to export for each loop execution to that array
  3. 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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay