Let’s say you make an api call to a backend to search for a row that meets some search criteria.
There are several cases that you know only a single row will meet that criteria. .e.g you were searching for a row with some unique value.
Even though you expect your result to return a single row, your endpoint may be returning a list of rows.
For example all calls made to bubble endpoints
/obj/thingname returns a list of
In bubble you will normally call the
:last function on a search result to get the first or last item. If you expect the result to have only a single item asking for the first or last item will return just that single item for you.
Unfortunately there is currently no feature like this in dropsource for us to get a specific element (first, last, or random) from a search result that is a list.
Tip and Trick:
So here is my workaround for this problem.
The idea is basically to use the
Iterate through Array action in the 200 response (or whatever your success response is) on the search result list.
First create a variable that is of the type that your search result returns.
- Add an
Iterate through Arrayaction
- Set the list property of the action to the event data then response result.
- Then inside the loop (
item) event set the variable defined is step 1 to the current item.
So essentially you’re telling dropsource to overwrite the value of the variable with the current item in each run of the iteration (loop).
But since your list has only one item, you’re simply going to run the loop only once.
What if my list returns more than one item but i want the first or last?
An extension of this tip is the case where your search result returns more than one item but you want to get say the first or last item.
In this case you will do the same thing as above only difference is that you will need to make sure your search result is sorted in a way that the item you’re interested in is the last item in the search result.
If you’re using Bubble backedn, you can define the sort order as well as the field to sort by using from the API Parameters tab in Dropsource by selecting the Query field.
And since you’re overwriting the value in every step of the iteration, at the end of the iteration value in the variable will be the last value.
And there is no performance hit even if your search result contains 100s of rows and you’re overwriting the variable in each step.
You can even avoid the repetitive overwriting of the value by doing it only once.
You could define a boolean variable called say
first_run and before the
Iterate through Array action set this value to false.
Then in the
item event of the iterate action, add an
if-else action that checks if
first_run is false then set your variable to the item value and also change the first_run to false.
This means that you will only write to your variable only once and in the first step because of the if-else condition.
I use these tricks and lot in my apps and they really work well.
Hopefully Dropsource will give us actions to get the first, last, or random item from a list of results soon. In the meantime lets keep hacking.