Hey there coders!
I have a tricky question and I hope som eadvanced javascript guru can read this.
I have one backend module called calculationService.jsw which I import into my script.
Inside that backend module I also import functions from dataService.jsw where I have all my data collection functions.
Now to the tricky part.
In my page I write this
let systemsizeactual = await system_size_actual(userData.user_monthlybill,regionData.region._id).toString()
console.log("systemsizeactual:" + systemsizeactual);
And inside that function in the calculationService.jsw I console log everything and it all looks perfect.
BUT!
The console.log functions inside that function is logging results after the value has been returned. So on my page I get back [object promise] as a value.
It seem that the function or call does not wait for all the jobs to be done before it returns object promise to me.
I use await and that should normally do it, right?
The function system_size_actual is below.
export async function system_size_actual(bill, regionId) {
// Calculation
// [(bill * 12)/electric_price]/[irradiance]
//
// Conditions
// -if bill > $200, multiply bill by .75
//
// Note
// Multiply Bill by 12. Divide result by Electric Price. Divide that result by Irradiance.
let result;
let electric_price = Number(await getElectricPriceByRegion(regionId));
let irradiance = Number(await getIrradianceByRegion(regionId));
if (bill > 200) {
bill = bill * 0.75;
}
result = bill * 12;
console.log("RESULT: " + result);
console.log("ELECTRIC PRICE: " + electric_price);
console.log("IRRADIANCE: " + irradiance);
result = result / electric_price;
console.log("RESULT: " + result);
result = result / irradiance;
console.log("RESULT: " + result);
result = await getTwoDecimals(result);
return result;
}
All the output in the functions console.log is perfect with the correct values but when calling it from my page I just get the object promise as value.
Please, someone… I am stuck with only this thing, everything else works like a charm.