Identifying User

Question:
I have something I am trying to accomplish and for simplicity I am breaking it down into parts. The first part requires me to identify the Mentor ID of the current signed-in site member. Mentor ID is a custom field in the Contacts database. So, what I need to do is assign the signed-in site member’s Mentor ID to a variable that can be used on various pages to represent the Mentor who is using the system. This way I can display only their information on various pages.

Product:
[Which editor or feature is your question most relevant to? e.g. Wix Editor, Wix Studio Editor, Editor X.]

What are you trying to achieve:
Assign the value of a custom Contacts field to a variable to be used in filtering CMS collections.

What have you already tried:
Nothing as I do not know where to start - new to Velo.

Additional information:

Have been messing with this since post - here is where I am at so far. This results in a contact not found error for some reason I do not understand.

Backed Code:

Front End Code:

Hi there,

In order to fetch the value of the custom fields, you do not need to query the user’s info using wix-crm. You can directly do it from the frontend using the wix-members-frontend API, by setting the fieldsets to “FULL”

Take a look at the code I’ve provided below:

import {currentMember, authentication} from 'wix-members-frontend';

$w.onReady(async function() {

    const isLoggedIn = authentication.loggedIn();

    if (isLoggedIn) {

        let options = {
            fieldsets: ['FULL']
        }

        let member = await currentMember.getMember(options);

        if (Object.keys(member.contactDetails.customFields).length > 0) {

            let value = member.contactDetails.customFields['custom.usertag'].value;

        } else {
           // Handle case if user does not have any custom fields added to their profile.
        }

    } else {
        // Handle case if user is not logged in.
    }

});

Just change usertag to the key of your custom field, and you’re good to go!

Thank you! Here’s what I have. I added a line to assign the Mentor ID to a text field for display. But the text field never updates to show value.

Field keys will probably not have a space in them, so I’d suggest that you first see the raw object and then copy the field key inside of it and use it in the code.

To see the raw object, replace the following code:

if (Object.keys(member.contactDetails.customFields).length > 0) {

 $w("#mentorIDtxt").text = JSON.stringify(member.contactDetails.customFields);

}

Thank you. So the MentorIDtxt element on the page is still not updating - in this case it is not changing to the field key information in your new line of code. I re-checked name of text element and it is correct.

That is weird… might be in the case if the Mentor ID value for the user is blank.

Can you check the output of this code then?

import {currentMember, authentication} from 'wix-members-frontend';

$w.onReady(async function() {

    const isLoggedIn = authentication.loggedIn();

    if (isLoggedIn) {

        let options = {
            fieldsets: ['FULL']
        }

        let member = await currentMember.getMember(options);
        $w("#mentorIDtxt").text = JSON.stringify(member.contactDetails);

    } else {
        // Handle case if user is not logged in.
    }

});

Ok so here’s what I did - you will see I embedded a few lines of code in different places to replace the text element with different values. I also created a couple more text elements for testing purposes.

When I run on live site, Mentor ID is never updated (no surprise as I still do not have key for field), contactDetails = {}, and testTxt = test01 which means it is not getting inside the second if statement.

yeah that confirms that the user does not have a mentor ID assigned. Can you try assigning it to the contact from the site dashboard and then check the result?

Also providing a screenshot of the user profile showing the custom field will help.

The user does have a Mentor ID as shown. I do not think we are even at that point yet, however as I still do not even know the field key for Mentor ID in the Contacts database to even know I am reading it correctly.

The code that I’ve shared is what I use in multiple sites of mine to retrieve the values of extended fields and it works perfectly. So there has to be something here that is being overlooked, and there’s only so much help I can provide by sharing screenshots and code snippets here. The reason I shared my code is because it is less complex to the alternative method, that uses wix-crm in the backend. But now that you’re unable to fetch it this way, I will recommend that you go with the initial approach and then check the results.

Also, a better practice while debugging your code would be to use console.log statements in place of using different text elements.

View Site Logs

I just need to figure out how to get the Contacts custom field key - I do not expect it to work yet as I do not know how to even get the key for the custom field.

Yes, you can fetch that using the example code snippet provided in the wix-crm-backend’s queryExtendedFields API.

Thanks for your help - will give it a shot.

I tried the code above and it does not recognize wix-web-module or wix-crm-backend.

Does not recognize? Are you getting some kind of an error?
Remember this code follows the new .web.js convention and not .jsw.

You can learn more about it here.

Additionally, if you’re using Wix Studio, you can ask the AI Code Assistant in the Wix IDE which can generate code for you based on your prompts.