nanami
March 13, 2021, 5:47am
1
I have a database like this
I want to display the “price” total.
my code is
wixData.aggregate( “gckj2gyh” )
.sum( “price” )
.run()
.then( (amount) => {
const sumTotal = 0 ;
let items = amount.items;
items.forEach(item =>{
{sumTotal+item.value;
}
});
$w( ‘#text10 ’ ).text = “” + sumTotal;
} )
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
} );
But always the total is 0.
please tell me how to fix this.
thank you for your readings
1 Like
What exactly do you want to count?
Here you are talking about ROWs which you want to count in a database.
That was my answer…
$w.onReady(async function() {
wixData.query("gckj2gyh")
.eq("title", true)
.count()
.then((num) => {
$w('#text9').text= String(num);
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
});
});
Or something like this one…?
var myDataset = '#dataset1'
var myTable = '#table1'
$w(myDataset).onReady(function(){
var total = 0;
for (var i = 0; i < $w(myTable).rows.length; i++) {
console.log($w(myTable).rows[i]["total"]);
total = Number(total) + Number($w(myTable).rows[i]["total"]);
console.log ("Total:" + total);
}
$w('#text12').text = String("$" + total);
});
@rlussian-dima , Thanks I am also looking for this script.
nanami
March 13, 2021, 2:50pm
4
Thank you for your reply.
Different for this post.
I want to display the “price” total.
This pattern display 600
And I don’t use dataset on this page.
Just I want to display total number
import wixData from'wix-data';
$w.onReady(async function() {
let myQuery = await wixData.query("gckj2gyh")
console.log("Query: ", myQuery)
myQuery.find()
.then((res) => {
console.log("Results: ", res)
$w('#text9').text= String(res.items.length);
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
});
});
nanami
March 13, 2021, 3:56pm
6
@russian-dima
Thank you again!
But this does not work…
because I want the sum of all the columns named “price”
This pattern, I want to display 600 (300+200+100)
@nanami
Ok, now i understand what you want to achieve…
Take a look onto the following example…(take a look into CONSOLE to see RESULTS.
let myItemsArray = [12, 1, 2, 5, 9]
$w.onReady(async function() {
let sum = 0
myItemsArray.forEach((item)=>{
sum = sum + item
console.log(sum)
});
})
Modified to your DATABASE…
$w.onReady(async function() {
wixData.query("gckj2gyh")
.find()
.then((res) => {
let items=res.items
let sum = 0
items.forEach((item)=>{
sum = sum + item
console.log(sum)
$w('#text9').text=String(sum);
});
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
});
})
nanami
March 13, 2021, 5:03pm
8
@russian-dima
Thank you for your thinking…
But no worked…
The “price” column will increase.
So the display number will change.
Your code not include “price”.
Is it not necessary to include the “price”?
What number [ 12 , 1 , 2 , 5 , 9 ]?
@nanami
Is it not necessary to include the “price”?
Exactly!
forgot to input —> .price → my bad,sorry.
sum = sum + item
This should fix the problem (not tested).
sum = sum + item.price
nanami
March 14, 2021, 3:06am
10
@russian-dima
Thank you a lot.
This is the result…; ;
sorry Is there a way to add?
@nanami
This happens because your “PRICE”-Field is setted up as…
----> STRING and NOT as ----> NUMBER.
Either you change it in your DB to the right type, or you do it in your code.
Since the price is a —> NUMBER you should change it in your DB.
To be 100% sure that the code works always, you can do both changes (in CODE + in DB).
sum = Number(sum) + Number(item.price)
This way, even if your “PRICE”-Field woulb be setted up to —> STRING/TEXT, the sum would work.
nanami
March 15, 2021, 1:31pm
12
@russian-dima
Sorry I’m late to check your comments.
It’s worked!!!
Wonderfull!!!
So many times, You are so kind.
Really really Thank you so much!