A bit of guidance needed - Datastore to server action

Hi,

Looking for a bit of guidance.

I have a Datastore with multiple records which I need to insert into a database wit that server action.

If anyone can recall seeing a post/guide or have the time to tell me on how to achieve this, could you post it here, please?

Thanks,
Ray.

1 Like

This tip from @ben might be helpful.

To add to Tom’s suggestion, have a look at https://github.com/Wappler/wCart/blob/master/_checkout.html

At the bottom of the document you will see the transfer of data to the database.

In a couple of minutes, you could have the wCart project running on your system, it contains a few handy tips.

1 Like

Actually a datastore save (post) to server connect action seems pretty trivial to me and often needed.

So maybe we should add a standard data store action for it. @patrick will check it out.

7 Likes

Hi Ben, Thanks for the info. I will check it out. I did recall briefly looking at this but thought you used an Array. Maybe Array/Datastore is the same method in gettig the data?

Thanks
Ray.

@George, that would be great, as I can see many occasions where I will be using the Datastore going forward with this project.

Ray.

Hi @ben, So after some head-scratching, I finally manage to de-code how you were using the Datastore/Form and passing the values back to a server action.

I thank you for making available you wCart which although I am not using, it has allowed me to progress my project.

Thanks, Ray.

1 Like

I second this. Spent the last hours trying to figure this out.

I am doing nested inserts. In the same spirit as the store example, (1) you want to POST order_id to the orders table, and (2) POST item_id’s in the Data Store to the items table. From looking through several posts, a lot of people are struggling with this.

In fact why bother with the hidden forms. Why can’t we provide a way to populate a data structure, similar to JSON with repeats and nesting. Then create an action to POST the structure to either database or API.

1 Like

Your solution saved me too! Thank you for open sourcing this!

2 Likes

In my experiment till date, I’m still staying with local storage & not using data store for two primary reasons:

  1. "Set" data from server connect: When using a local storage, I can easily set the results of a server connect to the local storage (both same schema) with a single liner like this:

{{local_storage.set('search_config',search_config.data.search_config)}}

while with data store, looks like this “Set” function is missing, and @patrick has given a workaround for this here - Set Data Store Value/Array from query in Server Connect. I would have thought data store being an abstraction over local storage didn’t lose this simple no-brainer set function.

  1. "Post" data to server connect: I’m aware of the hidden field post method, thanks to @ben’s amazing tutorials. The problem is, the moment you have more than one level in your data store (array of objects), the hidden field method becomes too cumbersome. I have spent hours making this work when I wanted to post a nested JSON by passionately following @nevil’s thread here - Send nested JSON with server-side API

When we talk about "post"ing data store, let’s not forget that we might need to post nested schema as well, in which case we are in fact posting “application/json” type.

Later, I just used the below javascript snippet, called it (from a static event) when I had to post nested json back to server, and I got rid of all the multi-dimensional array of hidden form fields.

function post_search_config(event) {

    // Sending and receiving data in JSON format using POST method

    var xhr = new XMLHttpRequest();
    var url = "http://localhost/dmxConnect/api/userprofile/search_config.php";
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-Type", "application/json");

    var search_config = dmx.parse('local_storage.data');

    var data = JSON.stringify(search_config);
    xhr.send(data);

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var response = JSON.parse(xhr.responseText);
            // things to do after getting a response for the json post
            dmx.parse('search_orders.load()');
            dmx.parse('flow_set_advanced_filtersort.run()');
        }
    };
}

Anyone wanting to post data store to server connect could use the above js snippet as an alternative to the hidden form fields method!

3 Likes

We have to standardize posting of any store data to server connect. Maybe @patrick can add more actions for this

1 Like

hello @George ,
It’s a really cleverly feature. Do you plan to add this feature soon. or will it take some more time?
of course if posible

Also bumping this too, hidden form fields are a bit tiresome for larger amounts of datastore data

2 Likes

bumping this request please.

1 Like

Me too, didn’t see this before making the below post as searched the forum a dozen times… Then lost the plot entirely…

Thanks for the heads up.

@patrick is already researching for better way to just post a data store record directly to a server connect action as input.

So that this can be easily achieved with an action of the data store itself.

5 Likes

Yes, I posted about the same thing last week and wasn’t able to find this post either. I can invent my own way but would prefer to use a good Wappler way!

What would be great is just one tutorial of what is possible at the code level right now. Reading through and trying to understand everyone’s different examples is driving me crazy and not giving me an answer!

2 Likes

We have just managed to do this. Will do a write up later. Quite simple, all came down to input names in our circumstance (finally we bound dynamic names to each input using the index). In the Multi-Insert (the array within the SC Action) the ‘record array’ variables must be named the same as the local storage items. That is about it… And obviously the insert variables must be named the same as the local storage items…

2 Likes

Fabulous @Dave!
Can’t wait to hear how you did it… I definitely need this functionality this week!