Dispalying a calculated sum in a page.

From another example my code below should display a total value in #text100.

import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
user.getEmail()
.then( (email) => {
let userEmail = email;
} );
$w.onReady(() => {
Sum_amount();
});
let having = wixData.filter().eq(“_owner”,userId)
export function Sum_amount(){
wixData.aggregate(“responses”)
.group(“_owner”)
.having(having)
.sum(“reward”,“sumHours”)

.run()
.then( (results) => {
$w(‘#text100’).text = results.items[0].sumHours; //display total in input field at bottom of table

});
}
How can i have #text100 display total value of values in column “responses”?

@paiy.surv, Try using the filter function instead because it looks like you want to pre-filter the results to match only the logged-in user’s hours. Having is used to filter the grouping results, something like the following:

let having = wixData.having().gt(“sumHours”,40)

That doesn’t appear to be what you are aiming to do.

Of course, this all presupposes that you have a field in the responses collection named “_owner” and it stores records pertaining to the logged-in user.

So it would go like this:

let filter = wixData.filter().eq("_owner",userId) 
export function Sum_amount(){ 
    wixData.aggregate("responses") 
    .group("_owner") 
    .filter(filter) 
    .sum("reward","sumHours")  
    .run() 
    .then( (results) => { 
        $w('#text100').text = results.items[0].sumHours;
    }); 
}

@tony-brunsman Thanks for the response. I have the field “_owner” but am still unable to get the desired result. Basically what am trying to do is to get the sum of values in the “reward” field specific to each loggedin user.

There might have been an error in which the text is displayed, this line
$w(’ #text100 ').text = results.items[0].sumHours;

Or the use of sumhours.

This is the database.

The numeric result needs to be converted into a string to display in the text element.

$w('#text100').text = results.items[0].sumHours.toString();

@tony-brunsman It has worked!! You deserve a beer! Cheers!

@tony-brunsman y The code only seems to work with me but when another user logs in, it doesn’t.
Does the filtered"_owner" refer to the admin only? And how do i go about to have each user get to have his sum displayed to him?