My code for the page is
import wixUsers from ‘wix-users’ ;
import wixLocation from ‘wix-location’ ;
import { assignRole } from ‘backend/role’ ;
$w . onReady ( function (){
$w ( ‘#register’ ). onClick ( function (){
let email = $w ( “#email” ). value ;
let password = $w ( “#password” ). value ;
let first = $w ( “#fname” ). value ;
let last = $w ( “#lastName” ). value ;
wixUsers . register ( email , password , {
contactInfo : {
“firstName” : first ,
“lastName” : last
}
})
. then (()=>
{
wixLocation . to ( ‘/dashboard1’ );
})
. then (() => {
let email = $w ( “#email” ). value ;
let roleId = “d990f333-d091-4670-ad29-f3a6d5e88f3f” ;
assignRole ( roleId , email );
}
)
}
)})
And the role.jsw code is
import { roles } from ‘wix-users-backend’ ;
export function assignRole ( roleId , email ) {
return roles . assignRole ( roleId , email , { suppressAuth : true })
. then ( () => {
console . log ( “Role assigned to member” );
})
. catch (( error ) => {
console . log ( error );
});
}
1 Like
From what I see in your page code (frontend), in the first .then() function after wixUsers.register , you redirect to the dashboard1 page. The second .then() function, where you assign the role, will never be executed.
1 Like
so whats the solution to this?
should I interchange the functions place
will that work?
1 Like
You can first call your assignRole() function, and then when the returned Promise is resolved, you can redirect to the dashboard1 page.
Keep in mind that when calling web modules (backend functions) from the frontend, you will need to handle the Promise. See the article Velo Web Modules: Calling Backend Code from the Frontend for details.
assignRole() function has two parameters :Role_Id and member_Id not email !
assignrole(roleId, memberId)
just change the member id parameter from email to member_Id
https://www.wix.com/velo/reference/wix-users-backend/roles-obj/assignrole
wixUsers.register(email, password,{
contactInfo:{"firstName": first,"lastName": last }})
.then((result)=>{
let roleId="d990f333-d091-4670-ad29-f3a6d5e88f3f";
assignRole(roleId, results.user.id);
wixLocation.to('/dashboard1');}).
}
try the code above it will work with you
1 Like
Hey @qoraia97 , good catch with the memberId (instead of member email). The only other thing that’s needed in your answer is to handle the Promise returned by assignRole() before performing the redirection, something like this:
assignRole(roleId, results.user.id)
.then(()=>{
wixLocation.to('/dashboard1');
})
@yisrael-wix Yes, you’re right, it’s good to add that