Inside a function I’m trying to retrieve values from different db collections and add them together to display on page.
I can console.log each individual result but I’m not sure how to call each result outside of a .then() and calculate a total price.
Should this be broken up into multiple functions? I’m a bit lost. Here’s my code so far.
function displayTotalCost() {
// query db on first collection and calculate first total
wixData.query(db_1)
.hasAll("foo", $w("#dropdown1").value)
.hasAll("bar", $w("#dropdown2").value)
.find()
.then((results) => {
const result = results.items[0]
const fee1 = result.db1CollectionField1
const fee2 = result.db1CollectionField2
const fee3 = result.db1CollectionField3
const tax = 0.05*(fee1 + fee2 + fee3)
const subtotal = (fee1 + fee2 + fee3)
const firstTotal = (subtotal + tax)
console.log(firstTotal)
})
.catch((err) => console.log(err));
// query db on second collection and calculate second total
wixData.query(db_2)
.hasAll("space", $w("#dropdown3").value)
.hasAll("balls", $w("#dropdown4").value
.find()
.then((results) => {
const result = results.items[0]
const fee1 = result.db2CollectionField1
const fee2 = result.db2CollectionField2
const fee3 = result.db2CollectionField3
const tax = 0.05*(fee1 + fee2 + fee3)
const subtotal = (fee1 + fee2 + fee3)
const secondTotal = (subtotal + tax)
console.log(secondTotal)
})
.catch((err) => console.log(err));
// add those totals together
// the below doesn't work
$w("#totalCost").text = firstTotal + secondTotal
}