Calling a backend function from the front end

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!

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

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

});

I’ve also tried this:

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

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

});

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

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.

Sure. Thanks!

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

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

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

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

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

Thanks!

Did the logs help you?