Hey Guys! How are you?. It would be really nice if you could help me a bit.
As summary, I am trying to make a WebSite based on ratings of different objects made by users. I got already a custom rate system that collects the rate of each one of the object and calculates the average per user.
Now i would like to calculate the total average for each object the users evaluate.
To do that I put here a code but it doesnt work sadly :(.
let count = ($w(“#dynamicDataset”).getTotalCount()) + 1; // count to calculate the average later
console.log(count)
wixData.query(“Opiniones”)
.eq(“nombreLabo”, $w(“#text7”).text) // Filtering the results by the specific object of the dynamic
page
.find()
.then((results) => {
console.log(results) // Shows all the rows that contains the object of the
dynamic page
let items = results.items
let value = items[i].Avg; // This should collect just numbers collected in the Average
(Avg) field from all of the filtered items in the array. Going row
per row indicated by the “i”
let sumTotal = 0
for ( var i = 0; i < value.length; i++) { // this loop should goes item per item of the Avg field
and sum them but it didnt work
sumTotal = sumTotal + value
}
console.log(value)
console.log(sum)
Thank you very much in advance. Any help would be really appreciated.
#sum #Databases #Average #query #Array #Filter
Hi, Jose Manuel Honrubia Belenguer .
I first formatted your code and, after pasting the formatted code here, I highlighted it all and clicked on the “Code Block” button (rightmost button) in the resultant toolbar:
let count = ($w("#dynamicDataset").getTotalCount()) + 1; // Count to calculate the average later
console.log(count)
wixData.query("Opiniones")
.eq("nombreLabo", $w("#text7").text) // Filtering the results by the specific object of the dynamic page
.find()
.then((results) => {
console.log(results); // Shows all the rows that contains the object of the dynamic page
let items = results.items;
let value = items[i].Avg; // This collects just numbers in the Average (Avg) field from all filtered items in array
let sumTotal = 0
for (var i = 0; i < value.length; i++) { // Loop through items in Avg field and sum them but it didn't work
sumTotal = sumTotal + value;
}
console.log(value);
console.log(sum);
I formatted the code in Notepad++; BTW, I try to leave console.log() calls out to the far left to make it obvious that they are for debugging purposes only).
When I counted the left parentheses vs. the right parentheses, I found there was 1 more left parenthesis (total of 15) than right parentheses (total of 14) and 1 more left curly brace (total of 2) than right curly braces (total of 1).
In reviewing your code, the key line appears to be:
.then((results) => {
I don’t see the closing/right curly brace nor the closing/right parenthesis in your code.
})
will need to be inserted … I believe after the following line:
console.log(sum);
Also, there is no variable named sum so I’m thinking that you probably meant sumTotal as follows:
console.log(sumTotal);
Does this help?
Aberquist! Thank you very much! Ups sorry, the closing/right was down. My bad there. After taking yur comments and put the:
"let values = item[i].Avg; " inside the for loop it worked!!
Thank you very much again! Im so happy right now 
Ah, yes, of course; i is only (defined and lives) in the loop. Good catch!
@josehonrubiabelengue where inside the for loop does it go?
@brian39460 Here:
for (var i = 0; i < value.length; i++) { // Loop through items in Avg field and sum them
let value = items[i].Avg
sumTotal = sumTotal + value;
}