Problem with js query

I am working on a GUI in retool, however I found a problem, I have a list of ids and i need to make a list of objects from them. The list input here is [1, 2], however the functions calls the get_stop_object query twice for the same input for some reason. Maybe there is some mistake i dont see or maybe i dont completly understand the await. I would apreciate any help, thank you

this looks incredibly complicated and I'm not 100% sure what outcome you're trying to achieve but...

my best guess is that your get_stop_object function uses the value of current_stop_id when it runs - but current_stop_id.setValue() will take a moment to complete but you're not awaiting that completion and so you are triggering get_stop_object at the same time and it's using the "wrong" value.

So I think the fix for this problem would be to change
current_stop_id.setValue(stopId); to await current_stop_id.setValue(stopId);

However, I think there are many more optimisations and simplifications to be made here too.

2 Likes

+1 for what @dcartlidge said. It seems that there's a lot of extra work happening here that doesn't need to.

For the specific issue you're asking about, you could also pass the ID to get_stop_object using additionalScope in the query trigger and not have to worry about the variable updating in sync with the function call.

for (const stopId of routeStops) {
   const updatedStopObject = await get_stop_object.trigger({additionalScope: {id: stopId}})
   updatedStopObjects.push(updatedStopObject)
}

Then in get_stop_object you'd simply reference {{id}} where you're currently referencing the variable.

1 Like

Okay, thank you very much the first resolve unfortunatly did not work for some reason, however this one did. Thank you!

1 Like

great that you managed to get it working - I would suggest that you take at simplifying your code for next time something needs to be changed, though. I've written many Retool apps and have had to use an async await block of promises like this maybe twice.

2 Likes