Repeating Insert Malfunctions

Hi there,
My multi-insert isn't working even though it works fine if I treat it as a single insert.

I have people enter an address on a page CustomerHome.php.
It goes to the Server Action "VerifyAddress."
That Server Action hits up an API from smartystreets.com.
After that there is an insert-record into the database.

And, it works just fine as long as it's a single record insert. But, when I use a multi-record insert nothing goes into the database (but the API says return was successful).

I watched a video and followed the advice here:

But, still nothing.
Any help is appreciated. Here is a screen shot if it helps.
Thanks,
Jeff

Hey Jeff, if I decipher this correctly you are not repeating over the results from your api action. To do that you first need to use the define schema option on the api action in order for Wappler to understand what the api is returning. Once you have that, you will set your repeat expression to be the array which should be repeated. API’s often return several arrays, so you just pick whatever skills drive the repeat.

Thank you!
I have done the “define schema” on the api action. Even had to add some manually because it didn’t get them all automatically.

But, can you help me understand what you mean by, “set your repeat expression to be the array which should be repeated. API’s often return several arrays, so you just pick whatever skills drive the repeat”?

Let’s first clarify what it is you WANT to see happen. It looks like the user provides an address (just one?) that you confirm against an api and then insert into db, correct?

Yes.
The user enters an address.
I send that address to an API (smartystreets.com). It will return the address, correctly formatted (and also broken up into pieces). For instance if you accidentally type:

1600 Pennsylvania Avenue Washington with no zip and forget the DC and NW…

It will figure out you meant:
*1600 Pennsylvania Avenue NW, *
Washington, DC 20500

It also returns things like:
Address number: 1600
Street Name: Pennsylvania
Street Suffix: Avenue

Sometimes it will return 2 or 3 potential “correct” addresses.
So, I was going to store them in the database and then display them to the user and allow the user to choose which address is actually theirs.

So, I’m trying to do a multiple insert to put all the api records in the database.

Great. I took a quick look at their api response, and it is an array at the root of the response. You can see this from their api demo. It starts with a square bracket, which indicates it is an array (one or more elements.)

So you’ll have something like this:

This will perform an insert for each item in the array.

1 Like

I haven't used this API, but I've used a similar one. When several addresses are found I put them into a select element for the user to choose from. I think this how they're typically displayed. It wasn't necessary to store the addresses first - I just used populated the select straight from the server action results.

Obviously the API you're using may not work in the same way, but I thought I would mention it in case there's a simpler approach.

This is great. Thanks mebeingken!

I was able to get it working.
I had to make a few other adjustments as well, but your point cleared up my biggest problem THANKS!

And TomD -
That’s a good idea. In this case I have to pay for each query, so if the user navigates away and comes back later I’d like to have the info stored so I don’t have to hit the API again.

However, that would be very useful for some other stuff I’m doing.
How did you get the select element to grab the api data when the api data was serve side?

With this API, it was quite straightforward. Server action:

image
.. with the api-key in the Query part, and the select:

<select id="select1" class="custom-select" dmx-bind:options="sc_addresses.data.api1.data.addresses" optiontext="$value.replace(&quot;, , &quot;, &quot;, &quot;)" optionvalue="$value"> <option value="">select address</option> </select>

1 Like