Issue with sorting by geo distance in API workflow

Hi folks :wave: I’m encountering a weird issue and could use a fresh set of eyes. Short version: I am trying to reference the nearest distance from a given address to a polyline in an API workflow. It should be pretty straightforward. I have the list of geographic addresses comprising the polyline and the reference address in my database. To get the desired value, I should be able to simply sort the polyline coordinates by their distance from the selected address, sort them by ascending value and then pop the first item off the list. This is what I have done, as you can see below. The same logic seems to work fine client-side, in a repeating group. However, using it in an API workflow returns an empty value. I’ve set the workflow to ignore privacy rules, so that should not be the issue. I’m really at a loss here. Anyone ever experienced something similar? Server logs are, of course, of no real help in this instance.

Screen Shot 2023-03-20 at 4.40.14 PM

Hmm. Can you make the distance calculation work at all on the backend? E.g. do a workflow that creates a new thing and set some field to a single (scalar) distance calculated this way?

That would (possibly) let us know whether those calls work at all in the backend.

I suspect that the issue here is that this is a case where that list expression (take list and get each item’s distance) doesn’t get waited for because each items distance is an individual API call. This would sort of be a bug in Bubble, but sort of not… :each item is a map function that expects there are already values at the field in question, but it’s not an iterator. For example, I can’t say (in general) :each item + 5, you know?

(This is something I harp at Bubble on all the time.)

So I think what’s happening here is the expression just gets ignored (or it appears the list is empty to bubble and so you just get an empty result. But since this works in the front end one might reasonably assume that it should work in the backend and (if it’s a fact that it doesn’t), this could reasonably be assumed to be a bug and should be reported.

You’d have to make a very simplified case to show how/why this fails.

3 Likes

So it seems that calculating distance for a single value works just fine in the backend. The problem arises when sorting on distance for a list, as you’ve suggested. I guess I’ll file a bug report, unless you can think of some workaround I’m overlooking. In the near term, I suppose this is a problem for list popper and friends, no?

Yeah, if the only solution is (ugh) to do a recursive workflow to get your list of distances.

One thing to try (also with List Popper and Friends) is to break this into parts using Flow State List SSAs.

First get your Search… Coordinates and put that in a Flow State List. Next get whatever Result step 1 is (I mean do the Action after the first Flow State.

Then, do the distance calculation (using the Flow State outputs). You might even chain another one in there to do the :sort:first item.

Worth a try.

1 Like

Goes without saying that if be interested to know if it works, @ts11.