How to retrieve parameters by route - Node JS

Adriano, what if you just enter it manually?

{{query.token}}

Returns nothing :pensive:

I believe parameters on nodejs model are not retrievable from App Connect in partials. This has going on for some time.

The only way right now is to retrieve them when the ejs is parsed on server as I explained previously. Depending on what you are trying to achieve this could be enough.

Strange that if I create a view and not a partial I can recover, I will make a video showing

Look this

but how can we solve this big problem? also with vers. 3.7.2 doesn’t work, can anyone point out a beginner solution? thanks

1 Like

Hey @Mark72it

I created a dirty little hack to solve this.

If you don’t have one yet create a custom.js file in public/js

Screenshot 2021-02-08 at 21.46.02

Call it in your layout file:

Drop this code into the custom.js file:

function getparams(mapto, pos) {
    var inpath = window.location.pathname;
    var inarr = inpath.split("/");
    var outval = inarr[pos];
    dmx.app.data.content[mapto].value = outval;
}

In your router settings setup you route. (it actually doesn’t matter what you call the param, it is about the structure:

Screenshot 2021-02-08 at 21.52.39

in the view you want to retrieve this param or params create the variable or variables you want to assign the params to:

Create a flow that auto loads and call the getparams(mapto, pos) function.
mapto = the created variable
pos is the position of the param
eg /account/paramvalue = /1/2

if you have for example /account/2345/678
create two variables and then run the function twice with the the correct values and positions for each url param.

Make sure you have the flow set to Auto Run

Your param(s) are now mapped to your set variables and you can use them anywhere in your code.

This is a much easier solution in my opinion.
You can do something like

<dmx-value id="var1" dmx-bind:value="<%= $_PARAM.parameter_name %>"></dmx-value>
<!-- in your example -->
<dmx-value id="linkcode" dmx-bind:value="<%= $_PARAM.linkedcode %>"></dmx-value>
1 Like

Excellent thanks.

Is much slicker than my solution when I get it to work.

If I add it to the view or layout page I get:

Screenshot 2021-02-09 at 09.00.21

What’s the piece of code? Just assigning it to a Wappler var?

Edit: I’ve just tested it in a partial and a layout and I have no issues retrieving the parameter.

Tested in layout and in view.

Screenshot 2021-02-09 at 09.50.32

you can see the value above using the js script

When using dmx-bind the value should be an expression, so add extra quotes.

<dmx-value id="linkcode" dmx-bind:value="'<%= $_PARAM.linkedcode %>'"></dmx-value>

Alternative you could just set a static value like:

<dmx-value id="linkcode" value="<%= $_PARAM.linkedcode %>"></dmx-value>
4 Likes

Ah yes! Missed the quotes.

THIS is an Important Page to read, NODERS :mask: :sweat_smile:

I’m Bookmarking.

It would be GREAT if a lot of these isolated SOLVED forum posts could be put into a couple of Video tutorials. These are real world problems with solutions particular to working in the Wappler & NODE.js environment, which is the Main Push now by Wappler management.

The funny thing is that this is not even a Wappler thing but it is actually part of how express and ejs templates work. Knowing this expands the knowledge center to a web search engine instead of limiting it to Wappler forum.

An “Under the hood” section in https://docsdev.wappler.io could be interesting so people know that they can expand their knowledge of Wappler by learning the underlying tech which it is built on if that is what they want to do.

4 Likes

So…to get the route params, I always need to create a variable to store its value?

The question is: where do you want to get the route param? server side or client side?

And it also depends whether you’re using layouts and content pages or partials.

If it’s client side then define query param on the app within the layout.
If it’s server side… well I haven’t figured it out yet…

1 Like

This works!!! it is resolved!!

{{query.token}}

As Alexandre told you, just put query.name_of_variable and it works :slight_smile: