I would like to create a referrals system whereby a user can invite friends to the website and be able to get the total number. Each referee enters referrer’s phone number which am planning to filter to get the number of referrals displayed to each user on #text33
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 filter = wixData.filter().eq(“_owner”,userId)
export function Sum_amount(){
wixData.aggregate(“custdetails”)
.group(“_owner”)
.filter(filter)
.sum(“phoneNumber”,“sumHours1”)
.run()
.then( (results) => {
const sumHrs = Number(filter.items[0].sumHours1);
$w(‘#dataset21’).setFilter( wixData.filter().contains(‘referer’,sumHrs))
.then((res) => {
let count = $w(“#dataset21”).getTotalCount();
$w(“#text33”).text = count.toString();
})})}
Anyone with an idea of how i can achieve this?
So what seems to be the issue?
@carlos-alvarez The code doesn’t bring the total count. I might have miss the correct code with either this line;
$w(’ #dataset21 ').setFilter( wixData.filter().contains(‘referer’,sumHrs)) .then((res) => {
or
let count = $w(" #dataset21 ").getTotalCount();
I am not sure on the code which can get me the value from the first filter.
Here’s what you have to do.
Client side:
onReady async Function {
dataset.onready{
let item = $w("#dataset21").getCurrentItem();
let count = await backendFunction(item);
}}
Backend
export async function backendFunction(item){
let results
wixData.query()
.eq(filter for referer user)
results = results
return results.count
}
Also keep in mind your site curren’t code doesn’t seem to be secure at all
@carlos-alvarez On the security I had disabled the buttons once a user fills in the referer.
My backend function hasn’t worked yet though. I seem not to get where the problem might be. I have not had an experience with backend modules.
Here is my backend code:
referral.jsw (web modules need to have a .jsw extension)
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(() => {
backendFunction();
});
let filter = wixData.filter().eq(“_owner”,userId)
export async function backendFunction(item){
wixData.aggregate(“custdetails”)
.group(“_owner”)
.filter(filter)
.sum(“referer”,“sumHours”)
.run()
.then( (results) => {
return results.count
;})}
AND Frontend code:
export async function backendFunction(items){
let count = await backendFunction(items);
$w(“#dataset1”).onReady( () => {
let item = $w(“#dataset21”).getCurrentItem();
$w(“#text33”).text = count.toString();
})}
Just make sure you write it properly it should work
import wixData from 'wix-data';
import wixUsers from 'wix-users';
export async function getCount() {
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
let email
user.getEmail() .then( (email) => {
userEmail = email});
//Check the API dcoumentation for Query and run one, for example: .eq(referral,userid)\\
let count = queryItems.count
return count
}
export {getCount} from backend/referral.jsw
Onready asyc / write it properly
let count = await getCount();
$w('#text').text= count.toString;
;})}