How do I get server connect values inside javascript?

Hi guys i’m trying to get a value from a server connect api to use inside javascript on my page but i’m always getting an empty value.

I searched on this forum and saw that to get values from server connect inside javascript you use
dmx.parse('my-server-connect')

I’ve tried that but always getting empty values.

Below is my server connect definition for the api.
The api connection works and was able to automatically generate the schema.

Then I added the server connect to the page as shown below.
image

I want to get the value in the field “renderedHtml” so i did the following in my javascript code:

console.log(dmx.parse('serverconnect1.data.connect_to_ff.data.renderedHtml'));

Unfortunately i’m not getting any output in the console even though when i check the network tab in the browser the server connect call was successful and the api returned data as shown below:

What am i doing wrong???

Thanks

Hey Sean,

Try wrapping your js in this:

$(document).ready(function($) {
// Your code here
});

Or if this is not a page load thing, execute your js on the success event of the server connect. In other words, I think your js is running before the server connect has.

–Ken

Thanks Ken for the reply.
So you’re saying the the dmx.parse('server-connect-name') is correct.
I’m still very new to wappler so wasn’t sure if that syntax is correct.

My code is inside document.ready so I’m going to try your second suggestion.

Thanks

I think your dmx call is correct. After the page loads drop into the console and execute your dmx.parse to see what you get.

The server connect might not be directly accessible.
Instead of dmx.parse try dmx.app.data.... in console to check if the server connect is even available.
It might be that if you are using SPA, you will have to qualify the server connect with the route’s name to access it.

1 Like

@mebeingken you were right about the server connect not loaded when the script run because I tried executing it in the browser console and it worked.

I tried calling the function in the on success event of the server connect initially it wasn’t working because i was adding it as a dynamic event.
I just tried adding it as a static event and everything works now.
Thanks a lot.

But now my question is what is the difference between static and dynamic events in wappler? I’m going to create a new post on it as all the two people I asked said they’re not sure.

Static events are used to run any custom js function.
Dynamic events are used to run the app connect components events, which are available in the dynamic data picker.

2 Likes

Thanks @Teodor, that makes sense.
Is this documented anywhere, I will like to read more on it because I’ve just wasted 2hrs :sleepy: because I didn’t know this.

It will also be great if for the sake of us beginners you add tooltips on hover to some of these things as you’ve done with some other elements. It’s really helpful especially for those of us who are trying to learn wappler on the fly by creating a real product.
Thanks

Hello and welcome @Seanhoots !
I invite you to upvote this feature request then : More info about format/syntax requirements when entering variables and expression etc
:wink:

It is now - lol

3 Likes

Sorry about that Sean. I actually meant to write static event since this bit me a while back… but clearly I didn’t. My bad.

You actually can use the dynamic event but the syntax changes. I’ve done this when passing arguments to functions that need to be dynamic. If I recall correctly it becomes

functionName({{dynamic value}});

And of course you can quote the value if needed.