Acessing data from back-end

Hi,
I put a procedure (jsw) in the backend of my site. this procedure is intended to retreive from a table the sum of a numeric field from all records, and send a mail with this sum number. (I will then make a cron job to fully automatise this)
I put a file AutoProcedures.jsw in the backend (at the first level) with a function called MailStatSubmission ()
I put some console.log to test from a button on one page, and it shows tha retrieve from the DB does not work.
The console.log resul shows an empty array, and not the sum as expected.
For information, the same routine runs fine at the opening of one page of my site. (used to fill a text field qith the sum)
What did I do wrong?

Claude

copy of back-end code:
import wixData from ‘wix-data’ ;
export function MailStatSubmission () { console . log ( “fonction MailStatSubmission in Backend/Autoprocedures” );
// calculate sum of inscriptions
let NBInscriptions = 0 ;
console . log ( “Nb Inscrits” + NBInscriptions ); wixData . aggregate ( “jobApplication01” )
. limit ( 1000 )
. sum ( “numero” , “sumamount” )
. run ()
. then ( results => {
console . log (" Results" , results ) ; } );

First of all, please catch the error in the backend, leave the contents of the error in the backend log with console.log(error) and check it at logs. Backend logs can be viewed from the dashboard. You can get there by selecting Logs from WixEditor's developer tools. 

wixData . aggregate ( “jobApplication01” )
. limit ( 1000 )
. sum ( “numero” , “sumamount” )
. run ()
. then ( results => {
console . log (" Results" , results ) ;
})
.catch((error) => {
console.log(error);
});

Thanks. I did that and there is no error catched !

Please show me your page code that calls this backend function.

This is currently behind a test buttn on a page, the time to debug.
After I will add a cron job and send the result by mail.

import { MailStatSubmission } from ‘backend/AutoProcedures’;

export function buttonTest_click(event) {

console.log("boutontest"); 
MailStatSubmission(); 

}

thank you!!
Please rewrite like as follows.

import { MailStatSubmission } from ‘backend/AutoProcedures’;

import { MailStatSubmission } from ‘backend/AutoProcedures.jsw’;

I changed in the way you suggested. Same change : an empty array and no error catched. This is not surprising because I was already entering the MailStatSubmission fonction (console.log included were displayed). What surprise me is that the same fonction, in the “onready” function executed at the opening of one page is working perfectly. It’s likely I am missing something, but what?

So, you means in cases execution in public page code that procedure can be done correctly?

I got it. Looking at the detail of the array received , I noticed that in debug mode it was looking to the sandbox database and this one was empty. I copied live data to sandbox and now it works !!!
Thank you for your help, your questions put me in the good direction.

Have you tried checking access permissions on the collection and checking if the collection contains data?

The first issues I see is that in the backend function you are not returning anything and then in your page code you are not handling the promise. All backend functions return a promise.

For an example of returning from the backend, you can refer to the boilerplate code in the jsw file that is populated whenever you create a new one.

Here is also an article about how to use backend functions and call them from the page code

Tha qas the problem (see my message 10 minutes ago) , I was looking to sandbox data and this was empty !!

Ah…congratulation! I just as I thought. because I also made that sandbox mistake before too. data in sandBox is used in Preview mode.

Thank you for this good advice. I was not realy looking that way, because at the end, what I want to do is a CROnJob that execute this request, and send a mail with the result found. I do not know how to handle the return code in that case.
As I said, In the mean time I found my error : the debug was looking to the sandbox that was empty!

Oh! In that case, you should look at our scheduled jobs functionality as it is better suited for cron and yes no need to return anything to the front end in that case. The page code you provided confused me as to the purpose of the code. Glad you figured it out!

https://support.wix.com/en/article/velo-scheduling-recurring-jobs

Sorry I used the word CRON but I wanted to speak about the standard jobs.config feature. Thanks for your comments.

The first problem fixed, I have nox another strange behaviour : The code after the wix-data retrieval is executed before retrieval is completed, so the mail contains zero as number. It is visible from the debug screen it is executing line 72-78 (preparation of mail content and send mail) before receiving the result of the retreive (57-58)
SO how to force the system to wait query completion before going on with the script?

maybe you should use async/await