While overwriting in Server Action

No, the purpose of while is exactly the same as in any other language. Its just different in terms of how output works as compared to a repeat step in Wappler.

@patrick
Is there a reason why the while step hasn’t got the option to output each iteration?

To get a result like this example, you could use:

image

Thanks Tom. Problem is I don’t know how many there will be.

This will output a list of all the values until the while condition is met. It isn’t dependent on knowing the number of loops. (Perhaps I’ve misunderstood the issue.)

Here is @patrick’s previous explanation:

Wouldn’t that only iterate 10 times? I might have 150 loops, I might have 1. If I knew how many loops I could just use repeat. Am I not understanding?

Oh - was that just an example? So maybe I could do

while {{next_page}}
API Action: api_copy
var api_full =. my_api + api_copy.data
Set Value next_page - {{api_copy.data.nextPageToken}}

That’s an idea although I’m not sure how to merge the arrays together.

Ah - ok. So it’s not doing what a normal While does. That’s a bummer but I’ll find a work around.

I have used a while loop in the past and written a custom formatter that combines the arrays that are received from a query/api call. I’ll see if I can find it

That would be great. Thx.

What server model are you using?

I’m using PHP

Here’s the custom formatter function:

function formatter_concatarr($arr1, $arr2){
    if(!is_array($arr1)){ $arr1 = array();} 
    if(!is_array($arr2)){ $arr2 = array();}
    $retval = array_merge($arr1,$arr2); 
return $retval; }

This is my server action file:

(note: each of the set values has a local and global name. It is the global one I will use)

I end up with one set of results (all pages of retrieved data) in an output value called items

Mine is not a Google call so you will need to adjust the pagination reference to the nextPageToken etc. but the sructure should get you closer to what you want

1 Like

Eventually we should make some kind of array store component also for server connect that you can use to init, add, append and output data in various steps. Maybe @patrick can check this.

1 Like

Thanks for this. Can’t seem to get it to work yet and Google just pinged me for going over my limit for the day so I’ll get back to it tomorrow. Just wanted to let you know I appreciate the help.

No problem. Happy to help.

Make sure for the Set Value steps you have a ‘Global Name’ set (I always make mine different to the local ‘Name’ property, just to be sure). Also make sure that when you use it later, you use the global name too. It won’t show in the picker so you have to type it by hand.

If you want to share your new API steps with me so I can have a look, please do.

Could we also have the option of adding a max iterations value to the while? (I can add it as a separate FR if required)

I can quickly think of two reasons it would be useful:

  • If you are expecting a change of data to break the loop and that doesn’t get set for some reason causing it to continue indefinitely
  • Testing (particularly 3rd party paginated API calls) without burning through a daily quota you might have

That would be great. I was calling the next_page from the wrong place so it was just repeating the same call. Figured it out but would be sweet if it could be set to a max to prevent that happening.

Got it! Turns out I was missing .mediaItems on the first Set Valy array_local =

Thanks again for your help @bpj !

Glad it’s all working for you