Hello,
I have a database called roles and in it 2 columns, 1 with user IDs called Owner and another one called userRole with a value (1 or 2). What I would like to do is when the member is on the homepage if the userRole has the value = 1 then I would like to show a button on the page, otherwise hide that button.
As I’m new and don’t really understand the coding yet I need a code for something like this:
if ( user is logged in AND his user ID matches the user ID in the Owner column AND the value of userRole = 1) then
show button2 on page
else
hide button2 on page
I’d be really grateful if someone could help me out. Thanks 
Hi Lucas.
Wanted to clarify your use case. Do you actually want to show the button to logged in site members and hide it for site visitors?
Regards,
Genry.
Hi Genry,
I want to show the button only to site members that have the value 1 in the userRole column from the database. Is it possible?
Hi Lucas.
Another clarification: when you mention ‘Owner’ field - do you mean to the build in Owner field?
Regards,
Genry.
Hi Lucas.
Assuming the button’s initial state is set to be hidden, the name of the element is button2 , and Owner field is referenced to the system built-in field _owner - you can place the following code on the page where the button is located.
import wixData from 'wix-data';
import wixUsers from 'wix-users';
const VISITOR = "Visitor";
$w.onReady(function () {
showButton2();
});
function showButton2() {
const currentUser = wixUsers.currentUser;
if (currentUser.role === VISITOR || !currentUser.loggedIn) return;
wixData.query('roles')
.eq('_owner', currentUser.id)
.find()
.then( result => {
if (result.items.length === 0) return;
if (result.items[0].userRole === 1)
$w('#button2').show();
})
.catch(err => {
console.log(err);
});
}
Hello Genry,
Thank you for your reply. The “Owner” field is the one built in.
I have copied the code on the page with the button, but it seems that the button doesn’t show up even though I have the userRole = 1 in the database.
Hi Lucas.
The code assumes that userRole field is of type Number. Is it so? If the type is Text a slight change should be done to the condition.
Instead of:
result.items[0].userRole === 1
try using:
result.items[0].userRole === '1'
Also, if there are still difficulties, please share the url of the site, so I could take a closer look.
Regards,
Genry.
Hi Genry,
That userRole field is a text field and now it works like a charm. Thank you very much for your help.