Setting button label to member's name

Hi, I’m trying to set a button to display the member’s name when logged in. However, the code has broken the rest of my code and also does not work and I’m not sure why?

Would anyone have any ideas?

See code below.

export function myGetCurrentMemberFunction ( options ) {
return currentMember . getMember ( options )
. then (( member ) => {
if ( wixUsers.currentUser.loggedIn ) {
var firstName = ${ member.contactDetails.firstName } ;
$w ( ‘#logIn’ ). label = “hi” + firstName ;
}
else {
$w ( ‘#logIn’ ). label = “login” ;
}
return member ;
})
. catch (( error ) => {
console . error ( error );
})
}

 function myGetCurrentMemberFunction(options) {
return currentMember.getMember(options)
   .then((member) => {
      if (member) {
         var firstName = member.contactDetails.firstName;
         $w('#logIn').label = "hi " + firstName;
      }
      else {
         $w('#logIn').label = "login";
      }
      return member;
   })
   .catch((error) => {
   console.error(error);
   })
}

Hi,

Thankyou for this answer! Unfortunately, it still will not work for me?

No idea why!?

@alloutapparel It might be something in the part of the code that you haven’t posted.

@jonatandor35 Hey, if you would not mind… please see snippet of my code below! :slight_smile:

///////////////////////////////////////////////////////////// IMPORTS /////////////////////////////////////////////////////////////
import { currentMember } from ‘wix-members-backend’ ;
import wixWindow from ‘wix-window’ ;
import wixUsers from ‘wix-users’ ;
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;
import wixAnimations from ‘wix-animations’ ;

///////////////////////////////////////////////////////////// VARIABES /////////////////////////////////////////////////////////////

const menuItems = {
man : {
button : ‘#mens’ ,
underline : ‘#mensLine’ ,
contentBox : ‘#mensBox
},
woman : {
button : ‘#womens’ ,
underline : ‘#womensLine’ ,
contentBox : ‘#womensBox
},
newArrival : {
button : ‘#newArrivals’ ,
underline : ‘#newArrivalsLine
},
sale : {
button : ‘#sale’ ,
underline : ‘#salesLine
},
}

///////////////////////////////////////////////////////////// MEGAMENU /////////////////////////////////////////////////////////////

$w . onReady ( function () {
myGetCurrentMemberFunction ()
initMegaMenu ()
});

function initMegaMenu () {
for ( let i in menuItems ) {
$w ( menuItems[i ]. button ). onMouseIn (() => {
openRelevantPanel ( menuItems[i ]);
});

  $w ( menuItems[i ]. contentBox ). onMouseOut (() => { 
     openRelevantPanel (); 
  }); 

}
}

async function openRelevantPanel ( hoverItem ) {
for ( let i in menuItems )
if ( menuItems[i ] === hoverItem ) {
$w ( menuItems[i ]. underline ). show ();
await $w ( menuItems[i ]. contentBox ). expand ();
wixAnimations . timeline ()
. add($w ( menuItems[i ]. contentBox ). children [ 0 ], { y : - 20 , opacity : 0 , duration : 0 })
. add($w ( menuItems[i ]. contentBox ). children [ 0 ], { y : 0 , opacity : 1 , duration : 500 })
. play ();
} else {
$w ( menuItems[i ]. underline ). hide ();
$w ( menuItems[i ]. contentBox ). collapse ();
}
}

///////////////////////////////////////////////////////////// SIGN UP BUTTON /////////////////////////////////////////////////////////////

function myGetCurrentMemberFunction ( options ) {
return currentMember . getMember ( options )
. then (( member ) => {
if ( member ) {
const firstName = member.contactDetails.firstName ;
$w ( ‘#logIn’ ). label = "hi " + firstName ;
}
else {
$w ( ‘#logIn’ ). label = “login” ;
}
return member ;
})
. catch (( error ) => {
console . error ( error );
})
}

export function logIn_mouseIn ( event ) {
$w ( ‘#logInLine’ ). expand ();
}
export function logIn_mouseOut ( event ) {
$w ( ‘#logInLine’ ). collapse ();
}

export function logIn_click ( event ) {
if ( wixUsers.currentUser.loggedIn ) {
wixUsers . logout ();
wixLocation . to ( ‘/home’ );
}
else {
wixWindow . openLightbox ( ‘Member Sign In’ );
}
}

//....
function myGetCurrentMemberFunction() {
return currentMember.getMember({fieldsets: [ 'FULL' ]})
//....

And add console.log() 's to locate the issue.

just found this in the console.

console.error(‘Cannot find module 'wix-members-backend' in 'public/pages/masterPage.js'’);

I’ve fixed it! had the wrong import. did not need the ‘backend’!

1 Like