Assign Role by Dropdown Menu

Howdy all,

So I have run into a snag in coding what is essentially a custom registration form.

I am letting the members app take care of basic details (contactinfo, password, and email), however, I am trying to create coding that essentially allows user input to determine the users role upon registration.

I have created the .jsw as seen below:

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);
 });
}

However, I can’t figure out the logic for the front end.

This is what the sign up currently looks like:

I am using #roleAssign dropdown, which shows 2 options (parent, coach). Ideally, if the user chooses parent they would be assign one role, and if coach is selected this would be assigned another.

I just can’t wrap by head around that logic.

I tried using this code but no luck:

import wixMembers from 'wix-members';
import wixLocation from 'wix-location';
import {assignRole} from 'backend/role'; 

//AUTO ROLE ASSIGN
$w.onReady(function () {
        $w('#create').onClick(function(){
                let profileRole = $w('#roleAssign').value;

                if(profileRole = "family"){
                        let roleID ="6078991c-c487-45fc-922f-309bb8312baf";
                                assignRole(roleID)

                }
                        
        }

});

Any help or pointers would be greatly appreciated.

Josh

Hello! If you test your backend function with hardcoded data, is it working as expected?

How would I test it?

Do you mean adding a function line to actually force the code to fire?

This article will explain how to test your code, but in short you can run your function from the backend by adding some data manually adn you will see the output. This will help you begin to debug the issue

https://support.wix.com/en/article/velo-about-functional-testing-in-the-backend

Okay thanks, once I’m home I’ll test the backend.

Will let you know the result!

@amandam

So. unfortunately it did not work.

Got this error code:

Error: Invalid JSON object. Check the JSON object syntax in the left panel and try again.

This was the actually line from the test:

{
    "roleId": "6078991c-c487-45fc-922f-309bb8312baf
",;
    "email": "jsg0813@gmail.com"
}

Yes, your JSON object is invalid meaning it’s not structured properly. This error is not specific to Velo and is just about JSON objects in general so you may google if you want to learn more, but here is an example of JSON object structure.

Alternatively, if you are unable to get your object structured properly, you can set the variables in your function instead of the test data area are variables and just remove them when you are done testing

{"roleID":"the-role-id-value", "email":"test@email.com"}

thank you so much for the help!

I do really appreciate it!