Loop: How can I use the result of the first HTTP Request in the next Execution?

Hello everyone,
I am also very new here and trial and error myself through.

Right now I got stuck in one scenario where I need an metadata-element of the Json of a HTTP Request to do the next call in a loop until the “after” - metadata-element is empty.

I tried to do the loop via IF, Set or Code Nodes to but always stumbled over the fact that I need an empty string parameter for next_page_id in the first call instead of a set Variable or a [not found] which I get, when the looping nodes were not executed yet. And then the “metadata”-“after” result as an input in the subsequent loops.

Please help me finding an elegant solution to this. :pray:

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 0.200.1
  • Database you’re using (default: SQLite): Supabase
  • Running n8n with the execution process: own
  • Running n8n via: n8n.cloud

How can I check about a status of an Item that was not executed yet before the first loop?
How to get back an empty value instead of a [not found]?

Hi @ourHDC, it seems the service from your screenshot requires authenticated requests so I can’t test this one myself.

I need an empty string parameter for next_page_id in the first call instead of a set Variable

This part should be doable though even without access to your API. You can use the conditional/ternary operator to check whether a value is available. If so, use it and if not use an empty string instead.

Here’s a quick example workflow:

The exact expression I have used here is {{ $json.page ? $json.page + 1 : '' }}. If $json.page is truthy I’ll use that value and add 1 to it (which is how the dummy API I have used here works), otherwise I am simply returning am empty string.

In your case the cursor might be nested a little deeper so you might also need optional chaining here (essentially using ?. instead of just . to access your fields) in order to avoid hitting an error.

Hope this makes sense and helps!

1 Like

Thank you so much @MutedJam or taking your time and looking into this!

Just tried this with your expression. Still I stumble over this error again:


how to avoid the “not found” and create/return an empty string instead in such loop-scenarios?

I suspect the error on your latest screenshot could be related to nodes inside the loop being executed on their own. Can you confirm which error you are getting when executing the workflow instead of a single node?

Thanks for your support @MutedJam .

Unfortunately I was not able to solve it with just one HTTP but as a workaround I separated the first call from the following calls:

that way there are no empty values

2 Likes

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