Filter Results and Sum Based on USER [SOLVED]

Hi Guys,

I’ve been cracking my head with this one for a week already with no results.
What I need is to filter the Ambassador Database to find all entries corresponding to the logged in user and the sum up the points that correspond to that user to display them on a box.

I really need help!!!


The database contains the ambassador, referenced from another database and then the points assigned as well. I need to gather all the points and then display here:


where the 550 is. I already reviewed the query() and aggregate but I cant figure out how!!!

Thanks!

@J. D. You think you can shed some light on this?

Anyone???

I guess you can do it, using aggregate:
https://www.wix.com/corvid/reference/wix-data.WixDataAggregate.html#sum

But I’ve never tried it, so I’ll post here and alternative way (maybe slower):

wixData.query("CollectionName)
.eq("ambassador", userId)
.limit(1000)
.find()
.then((r) => {
let items = r.items;
let sum = items.reduce((a,c) => a + c.points, 0);
$w("#text1").text = sum.toString();
})

@jonatandor35 Always a saviour man. Could you explain me the .then((r) => line? I don’t get exactly what are you definign with it

@andres r is result of the query (I could use “results” instead,but r is shorter).
Then I retrieved the items array out of the results, and used reduce() method to sum up.

@jonatandor35 is reducer or reducr? I don’t know if you wrote it wrong

@andres reduce()

@jonatandor35 Ok Let me try!

I get a 0 on the count :confused:

Here is how i have it… the user is logged in and goes to a dynamic page “dashboard” where he finds the campaigns at the bottom. then the zero to the left should be the sum of all points in the database.


the ambassador field is referenced to another database where the user is assigned a role and the amount of points is entered manually every time he completes a campaign

and here is the code

import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
import wixWindow from ‘wix-window’;
import wixUsers from ‘wix-users’;

var user = wixUsers.currentUser;
var userId = user.id;
var isLoggedIn = user.loggedIn;
var email = user.getEmail();

$w.onReady( function () {
$w(‘#ambassadorButton’).hide();

$w(“#repeater1”).onItemReady( ($w, AmbCamp) => {

console.log(AmbCamp.campaignStatus);     

if (AmbCamp.campaignStatus === true ) {

    $w("#openCampbutton").label = "Open"; 

} 

else {

    $w("#openCampButton").hide();   

} 

        }); 

});

// Sum up the points for each user and display in account

wixData.query(“AmbRewards”)
.eq(“amabssador”, userId)
.limit(1000)
.find()
.then((r) => {
let items = r.items
let sum = items.reduce((a,c) => a + c.points, 0);
$w(“#accountSum”).text = sum.toString();
})

@andres I don’t know how you built your ambassadors collection, but you didn’t set the IDs there to be the same ad the IDs in your PrivateMembersData collection you can expect to get no results.

@jonatandor35 no I didn’t set them to be the same. I just referenced the field “Ambassador” to the privatemembers collection and it throws me the email to search for them.
So, basically, I’m determining the user via the email. OR is there a way to just avoid that? I could eliminate the field ambassador but I just want to use it as a reference so I know to whom am I assigning the points… makes sense?

@andres Just to understand, is the field ambassador in the screenshot linked to the PrivateMember collection? If it is, it’s supposed to work.

You have a spelling error in the ambassador field ( amabssador )

@jonatandor35 yes, it is. Basically what I need is to filter by user and sum the points each time there is an entry in the database with that user email and then the points are displayed

@jonatandor35 hahaha ok, corrected the misspelling but still shows 0

@jonatandor35

@andres so I don’t know. It looks fine. Maybe you didn’t set the collection permission or didn’t sync with the the live site.

@jonatandor35 for some reason I just get 0. Do you think that is just not reading the user id? or should I maybe enter this in a different way? Maybe not reference the field?

@andres first of all, try to locate the problem. for example add:

//code…
.then((r) => {
console.log(r.items.length);
console.log(r.items);
let items = r.items;
let sum = items.reduce((a,c) => a + c.points, 0); 
console.log(sum);
$w("#accountSum").text = sum.toString(); 
})
.catch((err) => {
console.log(err);
})

and see what it logs