Variable not incrementing as expected

Hi @yisrael-wix , thanks for the reply and for your pointers. Sorry, not sure what I could do about the formatting, I’ve lifted my nice code from the IDE into the code block to post but it’s ended up like that!

You have a data.items.forEach() loop that checks how many results? Currently less than 10.

What is getJSON()? Are you sure it’s executing and actually returning something? Yes, this is returning JSON from an API endpoint as expected.

Are you sure that the ( length > initialNearbyHotspots ) condition is resolved as TRUE? Try putting a console.log() right after it to see if the execution of the code even gets there and to see what’s happening. The code block following the if condition resolved as TRUE is definitely executing when the condition is met, as I am seeing console . log ( id + " completed " + initialNearbyHotspots + " to " + length ); in my Site Events for those items where length > initialNearbyHotspots.

To double-check, putting console.log(itemsAmended) in the if block, it is showing an incrementing integer. Would this indicate that the issue is with when itemsAmended is being called in the finally block?

About my code overall, function() is being executed by the job scheduler once per hour. My aim is for it go through each item in the Alerts collection and check an API endpoint for each item to update the initialNearbyHotspots property with the length of the array at the respective endpoint, if it is bigger (this is the if block in question) I am using wixData.update to update the initialNearbyHotspots property of that item.

In the other case ( else ), I am performing wixData.update with the existing property values (i.e. those returned by the query) so that the last updated property changes for these items, too, without actually changing anything else about the item. This is in case of my Alerts collection containing >1,000 items, in which case I want the 1,000 items which haven’t been subjected to this code for the longest period to be returned in WixData.query - I hope that makes sense?

I will have a play around with my code to see if I can implement async/await in order to fix any problems, and try an alternative to forEach (I’ve just read that forEach doesn’t wait for Promises, which is a problem!) I think I could split up the function itself into smaller sub-functions, too :grinning:

P.S. your Promises, Promises link returns a 404 :wink: