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.
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:
@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.
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?
@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 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?