API Data Source running twice

The problem I have is that a dmx-api-datasource is processing twice. It is a simple API Data Source linking to a PHP file in the same directory. It simply creates a PDF file as shown in the last screenshot. The funny thing is that both PDFs are exactly the same apart from the first one if missing part of the filename which is returned from the first SC.

	<dmx-serverconnect id="sc_create_PDF_from_job_id" url="../dmxConnect/api/Reports/sa_create_PDF_from_job_id.php" dmx-param:job_id="query.job_id"></dmx-serverconnect>
	<dmx-serverconnect id="sc_create_PDF_imgs" url="../dmxConnect/api/Reports/sa_create_PDF_imgs.php" dmx-param:job_id="query.job_id"></dmx-serverconnect>
	<dmx-api-datasource id="api1" is="dmx-fetch" dmx-param:dirname="'pdf_template_AD_Report.php'" dmx-param:output="'repository/' + query.job_id + '/' + query.job_id + '_' + sc_create_PDF_from_job_id.data.q_create_PDF_from_job_id.index + '_AD_Report.pdf'" url="pdf_generate.php" dmx-param:job_id="query.job_id"></dmx-api-datasource>

The two dmx-serverconnects and the first of the dmx-api-datasource process together. Then, when they have completed the dmx-api-datasource processes again but this time with the result of

sc_create_PDF_from_job_id.data.q_create_PDF_from_job_id.index

being returned and included (it is missing in the first process).

image

Can anyone give me a clue as to why ‘pdf_generate.php’ might be running twice?

This has been happening for quite some time and so is not related to version changes.
Current set-up : SPA Pages, Windows 10, MySQL, PHP

EDIT:
I don’t know why this might help, but could it be something to do with “index”?
Is “index” a reserved word?
In my case, Index is the index registration number of a car and not the $index of the returned query.

Can anyone help or give me some guidance as to where to start looking to resolve this issue?

Sorry but it is not really clear what are you trying to achieve and what is wrong on the page.
Can you explain what’s your idea and what needs to happen on your page and in what order?

Hi Teodor, I was trying to do a screencapture video but my utility isn’t loading. I will send you login details and instructions by PM if that is OK.

Can you explain what needs to happen or what you need to happen on the page exactly and in what order exactly, that’s what’s not clear to me.

Morning Teodor, thanks for coming back to me.

Clicking on the “12. PDF” button opens “call_pdf_generate.php?job_id=4”

On opening, this page fetches data from two Server Connect queries

<dmx-serverconnect id="sc_create_PDF_from_job_id" url="../dmxConnect/api/Reports/sa_create_PDF_from_job_id.php" dmx-param:job_id="query.job_id"></dmx-serverconnect>
<dmx-serverconnect id="sc_create_PDF_imgs" url="../dmxConnect/api/Reports/sa_create_PDF_imgs.php" dmx-param:job_id="query.job_id"></dmx-serverconnect>

And then processes an API call

<dmx-api-datasource id="api1" is="dmx-fetch" dmx-param:dirname="'pdf_template_AD_Report.php'" dmx-param:output="'repository/' + query.job_id + '/' + query.job_id + '_' + sc_create_PDF_from_job_id.data.q_create_PDF_from_job_id.index + '_AD_Report.pdf'" url="pdf_generate.php" dmx-param:job_id="query.job_id"></dmx-api-datasource>

This API call opens “pdf_generate.php” and sends the values “dirname” and “output” to it.

“pdf_generate.php” is a PHP page and NOT a Wappler generated page. This page has php code from https://html2pdf.fr/

This page checks to see if there is a PDF already in the directory, deletes it if there is, generates a new fresh PDF and saves it to the directory.

Then on the original “call_pdf_generate.php?job_id=4” page a link is displayed for the user to click and view the PDF.

The first time “pdf_generate.php” runs it is missing the value of “index” in the URL from the first SC but it still creates a PDF and saves it to the directory

But when it processes a second time, that “index” value is included, creating a second PDF with the correct filename.

image

I really hope that helps. I am finding it difficult to explain.

Neil, all I am asking is - what do you expect to happen / what are you trying to achieve exactly?
Just a simple answer like - “I need this to run first, then that runs, then this and that needs to happen…”

The end result should be a single PDF file, in this case “4_AK10 WHF_AD_Report.pdf”

The first of the “pdf_generate.php” should not exist because it is missing the value of “index”.

Could it be running before the two Server Connects have finished?

That is what i am trying to understand with my last few posts - is there a relation between these server actions and the pdf generation?
Should one wait for the others? What is the relation?

Please just explain what needs to happen in what order and what are the relations between these steps …

Server Action “sa_create_PDF_from_job_id.php” is a single query which retrieves the data for a single job.
Server Action “sa_create_PDF_imgs.php” is a multiple query which retrieves all the image data for the same job (link URLs etc).
These need to complete before the API call.

Well then just make the first server action execute on page load and make the second server action and the API call “No Auto Load”.

On success of the first server action, call the second one.
On success of the second server action, call the API step.

OK great, I am starting to feel much happier about this, and I am really sorry for the poor communication earlier.

Can you please clarify how to “On success of the second server action, call the API step.”

Should this be via a Workflow? Sorry for the lack of understanding.

At the moment I just have this

image

Neil, there are Dynamic Events available for every server action. The Dynamic Events can be triggered - on success, on error etc.

Disable auto load for your api step (i.e. enable this checkbox):
Screenshot_15

Select your server action, add a dynamic event, select on success:

Run the API call on success…

This is pretty basic stuff, thought you already knew about dynamic events.
So only the first server action runs on page load, then it triggers the second server action on success and the second server actions runs the API call on success.

1 Like

Thanks Teodor, I don’t know if it’s my age, stupidity or what but sometimes the most simple of tasks become clouded and I need guidance. You’re right, I should have known this but it just didn’t click. Now you have shown me the light, it’s like magic and all becomes clear again. Sorry for wasting your time.

You need a good break after this little session :smile:

1 Like

Maybe it’s good to explain how the server actions work, probably it’s not well explained in our docs.

If you have 3 server actions (or API calls) set on page load - they load asynchronously. They don’t wait for the previous one to finish running in order to run. So they will load at the same time and if any of them needs data from the others it may not receive the data, as the other server actions may not yet be loaded.

So if you have a server action which needs data from other server actions you have two options:

  1. Use flows and add server actions there (do not use run in this case) they will be executed one after another (synchronously)
  2. Disable auto load for all server actions except for the first one. Then use the success dynamic event to run them in the order you need. I.e. the first one loads the second one on success etc.
3 Likes

Great explanation. Hopefully that bit of clarity will help me in the future. Cheers.

1 Like