Trouble With Passing Query Results to Page Flow's Javascript

I have the following javascript in my page within script tags:

function getClosestEventDate(eventObjects) {
    if (!eventObjects || eventObjects.length === 0) {
        //return null;
        console.log('empty object');
    }

...more javascript

I'm calling it successfully from a page flow because I can see the console message "empty object".

And the Argument being passed to my javascript is this:

But the script keeps coming back with "empty object". I've tried putting the query results into an array and passing the array as the Argument, but still no luck. What am I doing wrong?

The code is correct, but when does the script run? Are you sure it doesn't run before the data from the server action is available on the page?

1 Like

No, I'm no sure it runs at the correct time. I listed it after the server connect, mistakenly assuming it won't run until the server action completes.

If I were to move the server action inside the flow, my understanding is that this would then execute in sequence. Is that correct? In this use case, the server connect is used for two purposes: executing the JS script to get its return, and also to build a select component within the page. I need to compare the JS script output with each select option to determine if that option needs to be 'selected' upon page load.

Are there additional ways to make sure the page flow only executes after a server connect for the page?

You should simply call the flow on server connect > dynamic events > done and not set it to auto run. This way you can be sure the data is available.

That worked. :+1: