Loop through Collection and send using XMLHttpRequest()

Hi I am trying to figure out how to loop through client side storage collection, and then for each record post them to an api url.

i am trying to create a guaranteed message delivery loop. (basically if an api is not available the first time i try send data to it, add the raw message to an outgoing queue and send later when its back up or i am in cell tower range again.)

image

this does not work. as below error

image

i am thinking of using the html request flow object but cant see a loop through collection flow object? how would this normally be achived.

Hmm… Are you still experiencing this problem?

I’m not sure if we’ve tried this kind of looping before. Did you try with a single record if you managed to send a XMLHttpRequest properly, that you have the configuration and everything right?

I can’t comment much on the code itself as me and JS aren’t great pals, but the way we do offline support is by saving whatever data is needed into a sync queue on the device, a local resource, and then sync from there periodically, e.g. when a “sync” button is pressed, or when app is opened or something. This can be done by used readily made flow functions, so that’s what I would recommend, if that’s possible for you. That would require having a rest api endpoint where you could post the records.

Hi Mevi, yes this does not work, i would prefer not to have to use javascript as well, so if you have an example of the queuing you mention and how to store data in a collection if it failed to be sent and later send it one row at a time and only removing it if we get 200 OK for the api, this would be greatly appreciated. this rather a large blocker for us. i need to be able to guarantee delivery of all messages we send out.

this is proving very clunky for a data and api perspective, not all apis are for just CRUD.

I haven’t worked on this myself so I can’t unfortunately explain to you with great confidence, but I’ll tell you what my understanding of how this has been done in our apps is.

First of all, the data is stored in the app in three places. There is the API end-point data, which is fetched from and which is synced to when the app has network connection (and the user wants to sync, or the app has determined that it should sync now). There is the data stored in local storage which is where the data is stored from the API end-point, to where the user’s changes are saved, and from which the sync to the API end-point data is made. And then there is the app variable data, which is where the data is stored to be displayed in UI elements, which is modified according to user’s changes, and from which the changes to the locally stored data are made immediately, so as to not lose changes. The locally stored data is stored via the set/get storage flow functions.

The important thing is the comparison of the API end-point data to the data stored in local storage. There may be conflicts in these if the user is offline and changes were made to the backend from somewhere else, and you have to solve these in the app.

Unfortunately, the app I found had custom JavaScript boxes used as well, so I’m less confident now that this comparison and checking of the response can be done without JavaScript. Also since the app I found used fetch instead of XMLHttpRequest, I’m not sure how to apply your situation to it, but just to give you the general gist of it, the sync success check is done in that app as follows: