Need help with empty url parameters in server action

Hi all,

Breaking my head on this :exploding_head:, like many of us have according to the number of community posts on related issues.

Trying to filter a database query with some url parameter values, but can’t get that to work, although I checked all tutorial docs and community posts about this :nerd_face:

Found out that the parameters are just empty. The setup:

  1. Query Params are defined in the layout page and added then to the Query Manager:


    image

  2. This routing rule is set via the Routing Manager for the content page.
    Content page code:
    <meta name="ac:route" content="/:country/:language/product/:id/:product_name?">
    In Routing Manager:
    image

  3. In the server action with the database query execution in it, the same input variables are created under $_PARAM:
    image
    Also tried passing the input parameters from the query manager as a $_Get input to Server Connect, but these values also stay empty:

So for some reason the url parameter values are not passed to the server action side and I can’t find out why and how to solve this. Hope anyone can help, for which I’m very grateful in advance!

Are you on Node?

Yes on Node!

I’ll help you out, I’ve spent too much time on this in the past :wink:

So your goal is to for example, have an url like:

netherlands/nl/product/15

without url rewriting it would look like:
website.com/?country=netherlands&language=nl&product=15

Then you’d like to send all the params from the url, to a serverConnect that is autoloading on the page.

Correct?

Thanks so much for helping me on this, first of all!! Been spending hours on this already…

Yes, except for that the product part in this example is not a parameter:
netherlands/nl/product/15

Without URL rewriting the url would look like this:
site.com/product?country=US&language=en&product=15
The country & language parameters are moved to the front in our routing rule:
site.com/:country/:language/product/:id

Here we go, let’s start with how the url rewriting works.

URL rewriting and accessing the query parameters

I’m going to strip away Wappler’s UI first, so you can clearly see what’s happening.

  1. Create this route: /:country/:language/product/:product_id image

How it works: the variable, ex. :country, captures ANY word in it’s place and makes it available as {{query.country}}.
So the :country is basically the key. And the word in it’s place will be the value.
For example:
/norway will be: query.country = norway

Note: if you use this directly with a server action, they are available under $_PARAM. image

If you’re confused at this point, just keep reading and testing it yourself. It’ll make sense.

  1. After creating this route, go to the page that is linked to it. For me it’s simply called index image

  2. Put the query.key in your code. for example this is how it looks like for me:

<!-- Wappler include head-page="layouts/main" fontawesome_5="cdn" bootstrap5="local" is="dmx-app" id="index" appConnect="local" components="{dmxDatePicker:{}}" jquery_slim_35="cdn" moment_2="cdn" -->
<meta name="ac:route" content="/:country/:language/product/:product_id">

<div> This is query.country: {{query.country}}</div>
<div> This is query.language: {{query.language}}</div>
<div> This is query.product_id: {{query.product_id}}</div>
  1. Load the page in the browser. You’ll see it loads the URL without parameters filled in /:country/:language/product/:product_id

Then replace the parameters in the URL, and see what happens:

Note: I haven’t done ANYTHING except for the steps above.
That means that setting up the query parameters in the UI is actually optional.

Let’s do that now.

Setting up query params in the UI

  1. Go to your layout page connected to this (mine is ‘main’)
  2. Define the query params under app:
  3. Now the bindings are accessible using the UI. So you don’t need to type query.country but you can simply select it with the usual data binder.

Last step: getting this data to your server action.

Sending query params to your server action

  1. Create your server connect on the client side
  2. In your server connect, under $_GET set up your variables again. Note: you can name them differently now if you want, it doesn’t matter. I like to keep them the same to prevent confusion.
  3. Go back to your server connect (client side), refresh, and set the input parameters to use the query params
  4. Save everything, reload the page (make sure there are params in your URL!), open devtools and check the payload of your server action:
    You should see the parameters being sent as payload.
  5. Lastly, let’s see if the data is available in your server action:
    In the server action create a value my_data that has $_GET selected:
  6. Go back to your browser, refresh the page or replay the XHR:

You can now see the data is returned back. So your query parameters have successfully made a round trip from the browser, to your server, back to your browser :slight_smile:

3 Likes

@karh, thank you so much for taking the effort to explain this so extensively! :pray:

I got it to work now, but I don’t know exactly what is different from before and still got a few questions left.

First one is about this part:


Do you set the routing rule and change the type to Server Connect first and then change it to Page and select the specific content page or do you create both separately? The first method (changing it) worked for me btw.
I guess this did the trick, because I did exactly the same as in your tutorial before but then still the Get variables were empty in server connect. What do you think?

Secondly, this is what I was trying to achieve as well, but it still doesn’t work:

So I have to set and use the Get inputs unfortunately. Do you have any idea why the $_PARAM values are still empty?

Thanks again!!

1 Like

I haven’t changed the route type.
What I meant to say with that screenshot, is that IF you put it to server connect you can access the parameters in your Server Connect under $_PARAM.
So actually this is not relevant to your issue and I probably should’ve left it out to prevent confusion.

I simply used the automatically generated route and added the params in the ‘path’.

I think you’re expecting to use $_PARAM while not passing the query variables to the server connect.

Here’s a working example of using $_PARAM:

Output when using this route (see URL)

So the difference is, that in this case the route is loading a server connect.
In that case, the query params are available under $_PARAM.
But most of the time, we are loading pages first and put the server connect as a component on that page. Then we need to send the parameters to that server connect using query.variable as $_GET input variables.

2 Likes

Thanks, I did not interpret that correctly indeed, but it all makes sense now! Nevertheless thanks for working out the example for passing parameters to server connect directly, which makes totally clear that the $_PARAMS input is only available in such cases.

Appears now that when I tried passing the variables to server connect as $_GET query inputs, I used the Query Manager’s values for that which didn’t work. I had a similar problem once before.

Thanks a million again for your time and explanation, very much appreciated!! :+1: :+1:

1 Like

You’re welcome, I feel your pain. I’ve invested a lot of time figuring this all out in the past. So I believe you when you say

For the query manager: I’ve barely used it. Only when I want to change the query params on the page. In my example, you don’t need it. But you might need it for your whole use case though

1 Like

Still having trouble with this :grimacing: Hoping anybody can be of help here!!

I have a single product details page on which I want to use server connect data to server side render the dynamic page contents and met data as per this tutorial by @Teodor.

So on my server action the $_PARAMs are defined:

They are empty on output.

The server action url called from the product page is http://localhost:3000/api/Shop/GetProduct?country=US&language=en&product_id=11, however the database query returns no results when filtered on one of the set $_PARAMS since those are all empty for some reason.

So I tried creating a routing for the server connect action:
image
Full routing path is /api/Shop/GetProduct/:country/:language/:product_id

Still no luck and I can’t figure out what’s wrong and why the $_PARAMs stay empty in the server connect action.

If I call the same server action from the page by adding a server connect element to it and using $_GET variables everything works well, except it’s impossible to use the returned data in server side data to populate meta data, like here:
image
So that’s the reason I want to make the $_PARAMs work :slight_smile:

Any help would be very much appreciated, thanks in advance!

Okay, found out that this DOES work properly now after restarting Wappler :smiley:

Just one question left:
In order to bind data to other page components (like product name, description, price etc) on the product details page itself, is it necessary to add a server connect component to the page to run the same api call again to get these data available in the data binding picker or is there a way to use the output of this call as well?
image

These outputs are not available in the data picker, so in other words, should another GetProducts server connect component be added to the page that calls the same data with $_GET variables set? If so, no problem, but not having to call the same api twice on page load would be better of course.

They are, under server side data:

Yeah, got it! I was using dmx-text still, but completely makes sense now.

Thanks Teodor! :+1:

You failed to enter a name for your Set Value steps, you’d never see the output

Sorry Teodor, another question arose:

For an image src or repeat src expression for example, this is not possible, is it?

Suppose it’s best to use server side rendering only for page meta data that can’t be rendered client side and use another server connect component to render data for use in App Connect data bindings for everything else, including repeats and image sources, like @George described here, is that right?

Thanks @Apple, but that wasn’t the cause. I set the value names after I made the screenshot and then it still didn’t work until I restarted Wappler, sorry for the confusion. I guess the routing rules applied properly after restarting. Thanks for your reply though! :slight_smile:

1 Like