How to increment a variable?

Incrementing a variable used to gather all the pages of output.

I have an API I need to loop through multiple times until there’s an empty set given. So I use the ‘Set’ node to initialize a variable (PAGENUM) to 1.

Then I use that variable in the URL to retrieve the next page of data.

In the IF node, I test for the empty set I’ll get when there’s no more data, if it wasn’t empty, I use another set node to increment the page… But this doesn’t do it…

Hi there, it would help to see the workflow you are working on. Based on what you have posted however, I think it maybe one of the following:

I see you are referencing PAGENUM via the Json data instead of referencing the node it came from. Keep in mind, that every time it loops, that JSON changes, so the 2-3rd time you run it, the data structure looks different and it may not be on that key. You will instead want to reference the node itself, so that the json remains the same, and your JavaScript can reference it regularly. See example below

referencedata

  • {{ $json.test }} <----- Similar to what you have, referencing the global json data which can change
  • {{ $(‘Set’).item.json[“test”] }} <----- Same value as before, but referencing the node, which will keep referencing the data even if your global data has changed.

To easily pull the second version, I recommend you click on the little square box with arrow on the right of your {{($json.PAGENUM)+1}} to bring up the expression editor. On the left, you will see the JSON in question. Scroll down to see the nodes that you can reference. Click through until you find the data you want and click on it. It should generate the javascript reference for you. From there, add the +1 to the number.

As a word of caution, you may already know this, but if you are referencing a string, it may not work regardless. You need to convert the string into an integer using this command: {{parseInt($json["myString"])}} This will convert your string to an integer that can then do math operations on.

Hope this helps, and if it doesn’t please reply with a copy of your workflow to better assist.

2 Likes

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