Hi Community,
I’m working on a workflow in N8n where I need to paginate through a set of data by incrementing a page number after each loop iteration. My goal is to generate paginated URLs where each URL corresponds to a different page of data.
Here’s how my current workflow is set up:
-
Initial Setup:
- I start with an initial page number set to
1
in theEdit Fields
node. - The base URL and the page number are fed into a
Loop Over Items
node.
- I start with an initial page number set to
-
Code Node:
- Inside the
Loop Over Items
node, I use aCode
node to generate the paginated URL and increment the page number. Below is the code I’m using:
// Get the input data from the previous node const inputData = items[0].json; // Check if 'link' and 'page' are available in inputData if (!inputData.link || !inputData.page) { throw new Error("Missing 'link' or 'page' in input data"); } // Extract the base URL from the input data const baseUrl = inputData.link; // Define the page number you want to generate a URL for let pageNumber = inputData.page; // Calculate the start value for the pagination (assuming 10 items per page) const startValue = (pageNumber - 1) * 10; // Generate the new paginated URL const newUrl = `${baseUrl}&start=${startValue}`; // Increment the page number for the next iteration pageNumber += 1; // Return the new URL and the incremented page number return [ { json: { paginatedLink: newUrl, page: pageNumber } } ];
- Inside the
-
Expected Behavior:
- After the
Code
node runs, the page number should increment (e.g., from 1 to 2), and the workflow should continue to loop, generating a new URL for each subsequent page of data.
- After the
-
Issue:
- While the
Code
node generates the correct paginated URL and increments the page number, thepage
number in theEdit Fields
node does not update. It stays at1
instead of incrementing after each iteration.
- While the
-
Workflow Design:
- I’m connecting the output of the
Code
node back to theEdit Fields
node to update the page number for the next iteration, but the page number doesn’t seem to persist or carry over between iterations.
- I’m connecting the output of the
My Questions:
- How can I ensure that the
page
number updates correctly after each iteration in the workflow? - Is there a better way to design this workflow to handle pagination more effectively?
- Am I missing a step or configuration that would allow the
Edit Fields
node to correctly update and persist thepage
number for the next loop?
Any guidance, tips, or examples of how to implement this would be greatly appreciated!
Thank you!