Hello, hoping someone can help with this one.
I am trying to retrieve values from multiple datasets, multiply them together and total up the final result.
My code seems to fail at the multiply function, can anyone streamline this a bit?
export async function userStatsSpecificationValue(userId) {
var firstDayOfCurrentYear = new Date(new Date().getFullYear(), 0, 1);
let arrA = [];
let arrB = [];
let arrC = [];
wixData.query("projectproducts")
.eq("_owner", userId)
.eq("specified", true)
.ge("_createdDate", firstDayOfCurrentYear)
.find()
.then((results) => {
arrA = results.items;
for (var i = 0; i < results.items.length; i++) {
arrB.push({
"_id": arrA[i].product,
"productQuantity": arrA[i].productQuantity
});
let quant = arrA[i].productQuantity;
wixData.query('products')
.eq("_id", arrB._id)
.find()
.then((res) => {
let items = res.items;
let firstItem = res.items[0]; //see item below
let productId = firstItem._id;
let productPrice = firstItem.productPrice;
multiply(quant, productPrice).then(product => {
console.log(product);
arrC.push({
"value": product
});
// get all of the items
let sumTotal = 0; // declare sum
let values = arrC.items;
values.forEach(item => {
// the amount is a string so convert
sumTotal = sumTotal + Number(item.amount);
});
})
.catch(error => {
console.log(error);
});
})
.catch(error => {
console.log(error);
});
}
});
}
export function multiply(quant, productPrice) {
return quant * productPrice;
}
The dataset fields are:
dataset = projectproducts
field ID = product // matching ID in products dataset
field ID = productQuantity // returns number
dataset = products
field ID = _id // matching product field value in projectproducts dataset
field ID = productPrice // returns number
I am trying to query the projectproducts dataset and retrieve the items from the product field (several hundred of them) and the quantity from the productQuantity field.
Then based on the product field (which is the ID of the item/row in the products dataset ) query the products dataset and retrieve the items from productPrice field.
Then I’d like to multiply the productPrice x productQuantity and add all the values together eventually for a final amount, then return the result to a text field.
I am getting the following error in the preview log:
multiply(...).then is not a function
populateDashboard.jsw
Line 280