Question
Is it possible hide/show elements, in my case an image, based on member badge?
Product:
Wix Editor
What are you trying to achieve:
If logged member has “Sagittario” badge hide #img1. I need it for make the “classic” lock image on a content that disappear just if you have specific badge.
I’ve tried everything, I’ve been changing and replacing names, functions, APIs and variables for days; I’ve searched on Google, asked on the fb group, asked the AI, there’s nothing to do, that image that I put on my page to do this test doesn’t disappear. The log is always the same and it says that the collection doesn’t exist! Sorry but I’m really a beginner and self-taught in coding!
What have you already tried:
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;$w.onReady(function () {
// Check if the user is logged in
if (wixUsers.currentUser.loggedIn) {
const userId = wixUsers.currentUser.id; // Get the user ID// Query the Badges collection wixData.query("Badges") .eq("userId", userId) // Match the user ID .eq("badge", "Sagittario") // Check for the "Sagittario" badge .find() .then((results) => { if (results.items.length > 0) { // User has the badge, hide the image $w("#img1").hide(); console.log("Badge found. Image hidden."); } else { // User doesn't have the badge, show the image $w("#img1").show(); console.log("No badge found. Image shown."); } }) .catch((error) => { console.error("Error querying the Badges collection:", error); }); } else { // User is not logged in, show the image $w("#img1").show(); console.log("User not logged in. Image shown."); }
});
Additional information:
I find all this very strange because I have this other similar code on my site that always refers to the same collection and it works well… even if it always gives me the error that the collection doesn’t exist
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;$w.onReady(function () {
const user = wixUsers.currentUser;if (user.loggedIn) { const userId = user.id; wixData.query('Badges') .eq('userId', userId) .eq('badge', 'Sagittario') .find() .then(results => { if (results.items.length > 0) { $w('#statebox8').changeState('Si'); } else { $w('#statebox8').changeState('No'); } }) .catch((error) => { console.log('Error querying badges: ', error); }); } else { $w('#statebox8').changeState('No'); }
});
So thanks if you can help me solve this issue.