# Calling a backend function from the front end

**URL:** <https://forum.wixstudio.com/t/calling-a-backend-function-from-the-front-end/54494>\
**Category:** Ask the community\
**Tags:** api, code\
**Created:** [December 11, 2023, 2:34am UTC](https://forum.wixstudio.com/t/calling-a-backend-function-from-the-front-end/54494 "2023-12-11T02:34:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Len\_Perroots](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@Len\_Perroots](https://forum.wixstudio.com/u/Len_Perroots)\
**Post date:** [December 11, 2023, 2:34am UTC](https://forum.wixstudio.com/t/calling-a-backend-function-from-the-front-end/54494/1 "2023-12-11T02:34:05Z")

</div>

I am trying to populate a textbox from backend code upon entering the page. The data returns properly when outputted to the console… I just cannot get the value to be set on the page from the code. Everything looks correct. What am I missing? Thank you!

```javascript
import {getBusinessTotal} from 'backend/business
$w.onReady(async function () {

$w('#txt30').onViewportEnter(async () => {
     $w('#txt30').text = getBusinessTotal();
  
  });

});

```

---

<div class="post-metadata">

**Author:** ![Len\_Perroots](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@Len\_Perroots](https://forum.wixstudio.com/u/Len_Perroots)\
**Post date:** [December 11, 2023, 3:06am UTC](https://forum.wixstudio.com/t/calling-a-backend-function-from-the-front-end/54494/2 "2023-12-11T03:06:08Z")

</div>

> [@Len\_Perroots](#):
>
> ```javascript
> import {getBusinessTotal} from 'backend/business
> $w.onReady(async function () {
> 
> $w('#txt30').onViewportEnter(async () => {
> $w('#txt30').text = getBusinessTotal();
>   
> });
> 
> });
> 
> ```

I’ve also tried this:

```javascript
import {getBusinessTotal} from 'backend/business
$w.onReady(async function () {

$w('#txt30').onViewportEnter(async () => {
     $w('#txt30').text = await getBusinessTotal();
  
  });

});

```

---

<div class="post-metadata">

**Author:** ![Pratham](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/pratham/32/119586_2.png) [@Pratham](https://forum.wixstudio.com/u/Pratham)\
**Post date:** [December 11, 2023, 6:58am UTC](https://forum.wixstudio.com/t/calling-a-backend-function-from-the-front-end/54494/3 "2023-12-11T06:58:54Z")

</div>

Show us your backend code. We will not be able to get to the root of the issue without it! (:

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [December 11, 2023, 7:31am UTC](https://forum.wixstudio.com/t/calling-a-backend-function-from-the-front-end/54494/4 "2023-12-11T07:31:16Z")

</div>

> [@Len\_Perroots](#):
>
> ```javascript
> import {getBusinessTotal} from 'backend/business
> $w.onReady(async function () {
> 
> $w('#txt30').onViewportEnter(async () => {
> $w('#txt30').text = await getBusinessTotal();
>   
> });
> 
> });
> 
> ```

```javascript
import {getBusinessTotal} from 'backend/business

  $w.onReady(()=> {
      $w('#txt30').onViewportEnter(async()=> {
            let businessTotal = await getBusinessTotal(); console.log('B-Total: ', businessTotal);
            $w('#txt30').text = let businessTotal;
      });
 });

```

What do you get inside console for —\> **businessTotal**?

Do you return the values inside your BACKEND-CODE?  
Do you maybe use Wix-Data on Backend and you just forgot a second RETURN, happens often.

---

<div class="post-metadata">

**Author:** ![Len\_Perroots](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@Len\_Perroots](https://forum.wixstudio.com/u/Len_Perroots)\
**Post date:** [December 11, 2023, 8:11am UTC](https://forum.wixstudio.com/t/calling-a-backend-function-from-the-front-end/54494/6 "2023-12-11T08:11:54Z")

</div>

Sure. Thanks!

```javascript
import { sql } from '@velo/wix-data-sql-backend';

export async function getBusinessTotal(){

    try{
      
        let enddate = new Date().toISOString()
        enddate = enddate.substring(0, 10)
        let startdate = new Date()
        let startdate2 = subtractDays(startdate,30)
        let startdate3 = new Date(startdate2).toISOString()
        let startdate4 = startdate3.substring(0, 10)
     
        let mySQL = `SELECT sum(loanAmount) from Applications WHERE assigneeEmail = 'test@test.com' AND submitDate >= "${startdate4}"`
        const results = await sql(mySQL);

       let aResults = results.payload.data.rows;
        if(aResults.length > 0)
            return results.payload.data.rows[0].loanAmountSum
        else
            return 0
     
    } 
    catch (error) {
        return 0;
    }
}

```

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [December 11, 2023, 8:25am UTC](https://forum.wixstudio.com/t/calling-a-backend-function-from-the-front-end/54494/7 "2023-12-11T08:25:12Z")

</div>

Try to run this and inspect the OUTPUTS inside your CONSOLE…

```javascript
import { sql } from '@velo/wix-data-sql-backend';

export async function getBusinessTotal(){
    let consoleOutput = {};
    try{      
        let enddate = new Date().toISOString()
            enddate = enddate.substring(0, 10)
        let startdate = new Date(); consoleOutput.startdate1 = startdate;
        let startdate2 = subtractDays(startdate,30); consoleOutput.startdate2 = startdate2;
        let startdate3 = new Date(startdate2).toISOString(); consoleOutput.startdate3 = startdate3;
        let startdate4 = startdate3.substring(0, 10); consoleOutput.startdate4 = startdate4;
        //-------------------------------------------------------------------------------------------
        let mySQL = `SELECT sum(loanAmount) from Applications WHERE assigneeEmail = 'test@test.com' AND submitDate >= "${startdate4}"`
        const results = await sql(mySQL); consoleOutput.results = results;

        let aResults = results.payload.data.rows; consoleOutput.aResults = aResults;
        if(aResults.length > 0) {
            consoleOutput.aResultsTrue = aResults;
            return results.payload.data.rows[0].loanAmountSum;            
        }
        else {
            consoleOutput.catchError = undefined; 
            consoleOutput.catchValue = null;
            return consoleOutput;
        }
    } 
    catch(error) {
        consoleOutput.catchError = error; 
        consoleOutput.catchValue = null;
        return consoleOutput;
    }
}

```

missing function —\> **subtractDays(startdate,30);**

---

<div class="post-metadata">

**Author:** ![Len\_Perroots](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@Len\_Perroots](https://forum.wixstudio.com/u/Len_Perroots)\
**Post date:** [December 12, 2023, 6:04am UTC](https://forum.wixstudio.com/t/calling-a-backend-function-from-the-front-end/54494/8 "2023-12-12T06:04:56Z")

</div>

> [@CODE-NINJA](#):
>
> `$w('#txt30').text = let businessTotal;`

I was able to get it fixed by adding toString():

```javascript
$w('#txt30').text = let businessTotal.toString();

```

Thanks!

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [December 12, 2023, 6:22am UTC](https://forum.wixstudio.com/t/calling-a-backend-function-from-the-front-end/54494/9 "2023-12-12T06:22:05Z")

</div>

Did the logs help you?
