[Solved] assignRole() function doesnt work for me, is there anything I'm missing from here ? my code, seems normal but doesn't work still

import wixData from ‘wix-data’ ; import wixUsersBackend from ‘wix-users-backend’ ; import {roles} from ‘wix-users-backend’ ; exportfunction button1_click(event) { assignRole( “85aef24f-4a0b-4141-b40d-da3277c871cd” , “a2a28bfb-9383-4b7b-aa32-645959661a3e” ); } exportfunction assignRole(roleId, memberId) { return roles.assignRole(roleId, memberId, { suppressAuth: false }) .then( () => { console.log( “Role assigned to member” ); }) . catch ((error) => { console.log(error); }); }

You can’t use wixUsersBackend on the front-end.
You should create a jsw file on your backend and import the module to the front end.

1 Like

i did exactly that and still it doesnt work , this is my page code please any reply is very much appreciated .

import wixUsers from ‘wix-users’ ;
import wixData from ‘wix-data’ ;
import {assignRole} from ‘backend/aModule’ ;
import {removeRole} from ‘backend/aModule’ ;

export function button1_click(event) {
assignRole( ‘85aef24f-4a0b-4141-b40d-da3277c871cd’ , ‘a2a28bfb-9383-4b7b-aa32-645959661a3e’ );
removeRole( ‘85aef24f-4a0b-4141-b40d-da3277c871cd’ , ‘a717e157-7144-4efd-874a-9294d56795ad’ );

}

and this is my aModule.jsw file

// Filename: backend/aModule.jsw (web modules need to have a .jsw extension)

import wixData from ‘wix-data’ ;
import wixUsersBackend from ‘wix-users-backend’ ;
import {roles} from ‘wix-users-backend’ ;

export function assignRole(roleId, memberId) {
return roles.assignRole(roleId, memberId, { suppressAuth: false })
.then( () => {
console.log( “Role assigned to member” );
})
. catch ((error) => {
console.log(error);
});
}
export function removeRole(roleId, memberId) {
return roles.removeRole(roleId, memberId, { suppressAuth: false })
.then( () => {
console.log( “Role removed from member” );
})
. catch ((error) => {
console.log(error);
});
}

When you call a backend web module from the frontend, you need to handle the Promise. Something like this:

assignRole('85aef24f-4a0b-4141-b40d-da3277c871cd', 'a2a28bfb-9383-4b7b-aa32-645959661a3e', userId).then(resp => {
   console.log(resp);
})
.catch(error => {
   console.log(error);
});

removeRole('85aef24f-4a0b-4141-b40d-da3277c871cd', 'a717e157-7144-4efd-874a-9294d56795ad', userId).then(resp => {
   console.log(resp);
})
.catch(error => {
   console.log(error);
});
1 Like

wow youre really the best , but one last question , why adding ,userId to the function parameters , is it needed ? because i already gave the memberId in the function , which userId need to be used if so ?

1 Like

As you can see in the assignRole() API , the userid specifies what user is going to be assigned the role. If you already assign the appropriate userid in the backend function, then you don’t need to pass the backend function the userid.

1 Like

Where you able to get your setup to work? I’m working on something similar but
I’m personnaly getting back a 401 when i try to assign a role thru the backend?

Other question:
Is there a way to query the api from wix.dev in backend code without setting up oauth?

import { roles, wixUsersBackend } from ‘wix-users-backend’;

export function check(email, memberId) {
const members = [{
email: ‘hu’,
contactId: ‘2049aa3b-8’
},
{
email: ‘mom’,
contactId: ‘008456f’
},
]

console.log(‘whatnow’, memberId, email)
const roleId = ‘ca0e85’
members.some((e) => e.email === email)
? role(roleId, memberId)
: console.log(“no luck”);

}

function role(roleId, memberId) {
roles.assignRole(roleId, memberId, { suppressAuth: false })
.then(() => {
console.log(“Role assigned to member”);
})
.catch((error) => {
console.log(error);
});
}

ids and email have been sanitized… I’m using the correct ones =O