Question:
How do I set permissions for a backend function driving a progress bar using a members count?
Product:
Studio Editor, Velo
What are you trying to achieve:
I am creating a registration system which needs to be locked down once members have reached a certain number. I currently have functioning code for this, however it only works in the preview, not on the published site. I believe this is due to permissions not being set or something. I want to make it so anyone attempting to register can see how many spots are still available and once it’s filled, remove access to the registration system.
What have you already tried:
I am inexperienced with backend coding so I’m not even sure where to start.
Additional information:
Backend Function:
import wixData from 'wix-data';
// Function to get the total number of members
export async function getMembersCount() {
try {
// Query the Members/PrivateMembersData collection to get the count of members
const queryResult = await wixData.query("Members/PrivateMembersData").count();
// queryResult will return the total number of members
return queryResult;
} catch (error) {
console.error("Error in getMembersCount:", error);
throw new Error("Could not fetch members count");
}
}
Frontend function (wrapped in an onReady function with other code):
try {
// Fetch the total members from the backend function
const totalMembers = await getMembersCount();
console.log(totalMembers)
// Set your target number of members
const maxSpots = 50;
// Update the text element with the current number of members and available spots
$w('#membersProgressMessage').text = `${totalMembers} / ${maxSpots} spots available`;
} catch (error) {
console.error("Error fetching member count:", error);
$w('#membersProgressMessage').text = "Unable to load member data.";
}