Conditionally Shown Buttons

I want to conditionally show a button based on if a user has submitted to a database (here: alumni_profiles). The button (here: changeprofile) has been set to hidden on the properties panel. I have tried below code but it looks like the function is not running at all. Would someone be able to guide me into the correct direction?

Many thanks!
Anne

import { currentMember } from ‘wix-members’ ;
import wixData from ‘wix-data’ ;

$w . onReady ( function () {
let currentUser = currentMember ;
function checkDB ( ){
wixData . query ( “alumni_profiles” )
. eq ( “user” , currentUser )
. find ()
. then ( ( results ) => {
if ( results . items . length > 0 ){
$w ( ‘#changeprofile’ ). show ();
}
else {
$w ( ‘#changeprofile’ ). hide ();
}
} )
. catch ( ( err ) => {
let errorMsg = err ;
} );
}
})

Hi Anne - You need to either call the checkDB function or just delete the function checkDB () (and braces {}) statement if it isn’t called somewhere in your code. Also, check the rules for currentMember in the Velo reference API as I think you need to add .getMember() - getMember - Velo API Reference - Wix.com.

Hi Colin, thanks for your quick reply!

We implemented your feedback and I can confirm that the function is called now. However, the user cannot be found in the “Team2” database (I created a profile on the website, am logged in, and I created a row for myself in the Team2 database so would expect to see the #changeprofile button)- would you be able to help advise how we can resolve? Pasted the updated code below for your reference.

Thanks,
Anne

import { currentMember } from ‘wix-members’ ;
import wixData from ‘wix-data’ ;

$w . onReady ( function () {
let currentUser = currentMember . getMember ();
wixData . query ( “Team2” )
. eq ( “user” , currentUser )
. find ()
. then ( ( results ) => {
if ( results.items.length > 0 ){
$w ( ‘#changeprofile’ ). show ();
}
else {
$w ( ‘#input1’ ). show ();
$w ( ‘#createprofile’ ). show ();
}
} )
. catch ( ( err ) => {
let errorMsg = err ;
} );
})

Make sure you’re using the collection ID (maybe “team2” instead?) and adding console.log statements to view results may help.

Hi, thanks for your quick reply. I double-checked the collection ID and can confirm it should be Team2 as per above. I am getting the “failed to fetch” error and to confirm, I added below console.log which returns “undefined”. On this forum I have found some similar (unresolved) queries on this issue, would you be able to help advise how to resolve?

Thanks,
Anne

currentMember . getMember ()
. then (( member ) => {
console . log ( member );
});

https://www.wix.com/velo/forum/coding-with-velo/currentmember-getmember-getting-failed-to-fetch

Hi, I resolved this! Thanks all for your help.