I have a repeater that lists invoices. For each item there is a button that should link to a preview of the invoice. What is happening is the backend function returns a promise THEN retrieves each url. So the front end log shows the list of promises then a few seconds later, the back end log list of url’s. I need that to reverse.
This is my backend code and it works fine and the console shows the url’s.
export async function getURL ( myInvoiceId , myInvoiceVersion ) {
const id = {
id : myInvoiceId ,
version : myInvoiceVersion
};
const options = {
suppressAuth : true
};
let url = await invoices . createInvoicePreviewUrl ( id , options );
console . log ( url )
return url ;
}
This is the front end code.
import wixData from ‘wix-data’ ;
import wixUsers from ‘wix-users’ ;
import { getURL } from ‘backend/invoices’ ;
$w . onReady ( async function () {
$w ( '#repeaterInvoices' ). onItemReady (( $item , itemData , index ) => {
$item ( "#number" ). text = itemData . number ;
**let** d = itemData . issueDate ;
$item ( "#issueDate" ). text = d . toLocaleDateString ( "en-US" );
//The next 2 lines are the issue.
**let** invoiceURL = getURL ( itemData . _id , itemData . version );
console . log ( "URL = " + invoiceURL );
})
wixData . query ( "Billing/Invoices" )
. find ()
. then (( results ) => {
**if** ( results . totalCount > 0 ) {
$w ( '#repeaterInvoices' ). data = results . items ;
console . log ( "Query results:" , results . items );
}
})
. **catch** (( error ) => {
console . log ( "Error:" , error . message );
});
});
This is the console log
Query results:
[…]
URL = [object Promise]
URL = [object Promise]
URL = [object Promise]
URL = [object Promise]
URL = [object Promise]
(my url)
(my url)
(my url)
(my url)
(my url)
How can I get the highlighted lines to wait until the back end completes its function? Thank you.