I’ve got my code working on the preview side, but when I use it on the live site I get the following error:
[Error] Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating ‘mValues.dateRange’)
(anonymous function) (rvl2x.js:42)
promiseReactionJob
This is my code calling the server side function:
GetTaxInfo(nstartDate,nendDate).then ( function (mValues){
const myTableData = [
{“dateRange”: mValues.dateRange, “noSales”: 80, “totalSales”: 100, “totalTax”: 6}
];
$w("#table1").rows = myTableData;
});
And this is the server side code:
export async function GetTaxInfo(startDate,endDate){
v ar date1 = startDate;
var date2 = endDate;
try {
const results = await wixData.query(“Stores/Orders”)
.between(“_dateCreated”,date1,date2)
.find()
let mResults = results.items;
var x = 0;
var mTaxTotal = 0;
var mTotal = 0;
mResults.forEach( **function** (mSale){
var mTax = mSale.totals.tax
var mSub = mSale.totals.subtotal
mTotal += mSub;
mTaxTotal += mTax;
x++
});
let ostartDate = date1.getDate() + “/” + date1.getMonth() + “/” + date1.getFullYear();
let oendDate = date2.getDate() + “/” + date2.getMonth() + “/” + date2.getFullYear();
console.log(x + " records found. The was a Total of $" + mTotal + " in sales from " + startDate + " and " + endDate + ". The total tax paid was $" + mTaxTotal.toFixed(2) + ".");
//console.log(x + " records were found.");
return {
sales: mTotal,
tax: mTaxTotal.toFixed(2),
dateRange: ostartDate + " to " + oendDate,
nEntries: x,
};
}
catch (error){
console.log("Error: " + error);
}
}