Dmx.parse server connect to insert database record in a loop overwriting

I’m trying to loop through some data in Javascript and then insert into the database using dmx.parse. When it’s done running only the last item is in the database. If I stop it just after the first run it’s the first item so it seems it’s definitely overwriting. I also have a console.log outputting on each iteration and that is working.

dmx.parse(“sc_insert_daily_totals.load({date: '” + my_date + “’, open: '” + max_new_start_time + “’, close: '” + max_new_end_time + "’, top: " + max_new_total + “})”);

I tried adding dmx.requestUpdate(); after but that didn’t do anything.

Any thoughts?

Server connect can run only once. If you re-run it, the previous request gets cancelled.
You can check in the network tab.

Sometimes, requests that show up as cancelled, still get processed on the server. Just the browser does not wait for a response.
But that is not the case here, since you are calling it very frequently, even before previous request goes out of the browser, its probably getting cancelled.

Solution:
You need to send the data array at once and not loop it on client side.
You can use JSON.stringify to set it as a single param… and then use toJSON formatter on this input in server action to convert it into an array. Then run a loop on SA to process each row of the array.

Thanks @sid I don’t believe it’s getting cancelled as I can stop it after x number of iterations and what is in the database is the data from where it stopped. I can create an array and send it that way just extra learning/work. I do wonder if there is a way to force the browser to wait for a response though.

You can call the server action once, and on success event you can run your JS which will do the next call.
You can use both dynamic & static success event for this.