# Acessing data from back-end

**URL:** <https://forum.wixstudio.com/t/acessing-data-from-back-end/34037>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [October 11, 2022, 6:56am UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037 "2022-10-11T06:56:35Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![c-verstichel](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/c-verstichel/32/35007_2.png) [@c-verstichel](https://forum.wixstudio.com/u/c-verstichel)\
**Post date:** [October 11, 2022, 6:56am UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/1 "2022-10-11T06:56:35Z")

</div>

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 ) ; } );

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 11, 2022, 8:16am UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/2 "2022-10-11T08:16:44Z")

</div>

```
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);  
});

---

<div class="post-metadata">

**Author:** ![c-verstichel](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/c-verstichel/32/35007_2.png) [@c-verstichel](https://forum.wixstudio.com/u/c-verstichel)\
**Post date:** [October 11, 2022, 9:51am UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/3 "2022-10-11T09:51:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 11, 2022, 11:09am UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/4 "2022-10-11T11:09:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![c-verstichel](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/c-verstichel/32/35007_2.png) [@c-verstichel](https://forum.wixstudio.com/u/c-verstichel)\
**Post date:** [October 11, 2022, 11:54am UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/5 "2022-10-11T11:54:37Z")

</div>

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(); 

```

}

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 11, 2022, 12:31pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/6 "2022-10-11T12:31:40Z")

</div>

thank you!!  
Please rewrite like as follows.

import { MailStatSubmission } from ‘backend/AutoProcedures’;

→

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

---

<div class="post-metadata">

**Author:** ![c-verstichel](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/c-verstichel/32/35007_2.png) [@c-verstichel](https://forum.wixstudio.com/u/c-verstichel)\
**Post date:** [October 11, 2022, 3:07pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/7 "2022-10-11T15:07:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 11, 2022, 3:24pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/8 "2022-10-11T15:24:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![c-verstichel](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/c-verstichel/32/35007_2.png) [@c-verstichel](https://forum.wixstudio.com/u/c-verstichel)\
**Post date:** [October 11, 2022, 3:27pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/9 "2022-10-11T15:27:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 11, 2022, 3:31pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/10 "2022-10-11T15:31:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![amandam](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amandam/32/79539_2.png) [@amandam](https://forum.wixstudio.com/u/amandam)\
**Post date:** [October 11, 2022, 3:33pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/11 "2022-10-11T15:33:42Z")

</div>

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](https://support.wix.com/en/article/velo-web-modules-calling-backend-code-from-the-frontend) about how to use backend functions and call them from the page code

---

<div class="post-metadata">

**Author:** ![c-verstichel](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/c-verstichel/32/35007_2.png) [@c-verstichel](https://forum.wixstudio.com/u/c-verstichel)\
**Post date:** [October 11, 2022, 3:37pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/12 "2022-10-11T15:37:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 11, 2022, 3:41pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/13 "2022-10-11T15:41:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![c-verstichel](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/c-verstichel/32/35007_2.png) [@c-verstichel](https://forum.wixstudio.com/u/c-verstichel)\
**Post date:** [October 11, 2022, 3:52pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/14 "2022-10-11T15:52:36Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![amandam](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/amandam/32/79539_2.png) [@amandam](https://forum.wixstudio.com/u/amandam)\
**Post date:** [October 11, 2022, 4:06pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/15 "2022-10-11T16:06:25Z")

</div>

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](https://support.wix.com/en/article/velo-scheduling-recurring-jobs)

---

<div class="post-metadata">

**Author:** ![c-verstichel](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/c-verstichel/32/35007_2.png) [@c-verstichel](https://forum.wixstudio.com/u/c-verstichel)\
**Post date:** [October 11, 2022, 5:01pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/16 "2022-10-11T17:01:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![c-verstichel](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/c-verstichel/32/35007_2.png) [@c-verstichel](https://forum.wixstudio.com/u/c-verstichel)\
**Post date:** [October 11, 2022, 5:09pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/17 "2022-10-11T17:09:05Z")

</div>

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?

![](https://us1.discourse-cdn.com/wix/original/3X/4/2/421977e8a4b1209f55d39f6712fbfc5189d4a017.png)

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [October 11, 2022, 5:20pm UTC](https://forum.wixstudio.com/t/acessing-data-from-back-end/34037/18 "2022-10-11T17:20:01Z")

</div>

maybe you should use async/await
