wixLocation.To() on button click

Any help would be appreciated. Trying to open a page on button click based on the first Member role. The interpreter is telling me I am a moron.

import  wixUsers  from  'wix-users' ; 
import  wixLocation  from  'wix-location' ; 

$w.onReady( function  () { 
 
 if  (currentUser.loggedIn)  
    $w( '#PartnerSharebtn' ).show(); 
 **else** 
    $w( '#PartnerSharebtn' ).hide(); 

}); 

onLogin(() => { 
  $w( '#PartnerSharebtn' ).show(); 
}); 

export function  PartnerSharebtn_click(event) { 

 const  usr = currentUser; 

  usr.getRoles() 
    .then( (roles) => {        
 **switch**  (roles[ 0 ].name) { 
 **case**  'ADMIN'  : 
 **case**  'VAR'  : 
          wixLocation.to( "https://microsoft.com" ); 
 break ; 
 **case**  'Avocor' 
          wixLocation.to( "https://microsoft.com" ); 
 break ;   
 **default**  : 
 break ; 
      } 
     } 
  } ); 
}

Hi,
I have checked you code and I see a number of issues.
Please see a working code example below:

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

$w.onReady(function () {})

export function button9_click(event) {
 const user = wixUsers.currentUser;
    user.getRoles()
        .then((roles) => {
 switch (roles[0].name) {
 case 'Admin': //name of the role
                wixLocation.to("https://microsoft.com");
 break;
 case 'test': //name of another role;
                wixLocation.to("https://microsoft.com");
 break;
 default:
                wixLocation.to("https://microsoft.com");
            }
        })

}
  1. To get the current user you need to use the wixUser.currentUser;

  2. Names of the roles are case sensitive, so should be exactly like in dashboard (e.g. Admin);

  3. The code works only on the live site, therefore you won’t be redirected in the editor;

  4. Please note that roles[0] gets only the role that appeared first for the user. If you wish to check all the users’ roles you need to use a different syntax. Click here to learn more about the getRoles() function.

Thank you very much for the help. I will try to improve the quality of my errors.

:rofl:

is there new redirection method in the new updates

Hey @user909!

This topic is pretty old. To help keep the forum organised, please open a new topic outlining what you’re facing, what you’re hoping to achieve and what you’ve already tried.