How to get custom field from a contact?

I am trying to get the Contact Custom field “Schools” and “Member Super ID” from contact to replace #memberUpId element

I used a static string “Hello!” for test which it works.

$w.onReady(function () {
    let memberId = $w("#memberUpId"); // Select the text element
    
    if (!memberId) {
        console.error("❌ Error: Element #memberUpId not found.");
        return; // Stop execution if the element doesn't exist
    }

    memberId.text = "Hello!"; // Update the text
    memberId.show(); // Make sure it's visible
    
    console.log("✅ Updated #memberUpId with 'Hello'");
});

I tried wix-members-frontend
and able to get below fields
const contactId = member.contactId;
const contactEmail = ${member.loginEmail};

But I cannot figure out how to get the custom field…

I have been trying for 3 hours… Any help would be appreciated.
Thanks

Take a look at this API Get Contact | Velo

Hello

I am trying to get the custom field value for the current member, pre-populate the #memberUpId text element. However I am not sure getContact is the right one…

import { contacts } from "wix-crm.v2";
import wixUsers from "wix-users";

export async function myGetContactFunction() {
  try {
    const user = wixUsers.currentUser;

    if (!user.loggedIn) {
      $w("#memberUpId").text = "Please log in to see your Member ID.";
      return;
    }

    // Get the user's email
    const userEmail = await user.getEmail();
    console.log("User Email:", userEmail);

    if (!userEmail) {
      throw new Error("User email not found.");
    }

    // Fetch the contact using the email
    const contact = await contacts.getContactByEmail(userEmail);
    if (!contact) {
      throw new Error("No contact found in Wix CRM.");
    }

    const contactId = contact._id;
    console.log("Member ID:", contactId);

    // Update the text element with the contact ID
    $w("#memberUpId").text = `Member ID: ${contactId}`;

    return contact;
  } catch (error) {
    console.error("Error fetching contact:", error);
    $w("#memberUpId").text = "Error retrieving Member ID"; // Display error message on UI
  }
}

// Fetch and populate the contact ID when the page loads
$w.onReady(async function () {
  await myGetContactFunction();
});

It does not work when I am trying to get the email from the current contact.

Are you sure it works and should I keep trying?

Thanks

Query Contacts | Velo use this instead for a specific contact email / id and you should get the extendedfields

2 Likes

Hello I want to simplify my question

My task is not complex. I have a custom field on the backend called “member up id,” which is assigned by the admin to the member (see pic 1).

I want this “member up id” value to be displayed in a text element “#memberUpId” on the member account dashboard (see pic 2).

It’s not possible to achieve this with the Wix Members Frontend API. I’m curious if anyone has attempted something similar and could guide me in the right direction using the correct API. I’ve been experimenting with various APIs for days and have spent too much time on it. Look for a shortcut…

Thanks

:white_check_mark: Solution: Use wix-crm to Get Custom Fields

Since custom fields belong to the Contacts collection, you need to use the wix-crm module to fetch them. Here’s the correct way to get the custom fields and display them in #memberUpId:


Step-by-Step Fix

:one: Import the Required Modules

Add this at the top of your code:

javascript

CopyEdit

import { currentMember } from 'wix-members-frontend';
import { getContactById } from 'wix-crm-backend';

:two: Get the Logged-in Member’s Contact ID

javascript

CopyEdit

$w.onReady(async function () {
    let memberUpId = $w("#memberUpId"); // Select the text element

    if (!memberUpId) {
        console.error("❌ Error: Element #memberUpId not found.");
        return;
    }

    try {
        // Get the currently logged-in member
        let member = await currentMember.getMember();

        if (!member) {
            console.error("❌ Error: No member is logged in.");
            return;
        }

        const contactId = member.contactId; // Get the contact ID
        console.log("✅ Contact ID:", contactId);

        // Fetch contact details including custom fields
        let contact = await getContactById(contactId);

        if (!contact) {
            console.error("❌ Error: Contact not found.");
            return;
        }

        // Extract custom fields
        let schools = contact.customFields["Schools"] || "No School Info";
        let memberSuperId = contact.customFields["Member Super ID"] || "No Super ID";

        // Update the page with the retrieved data
        memberUpId.text = `School: ${schools} | Super ID: ${memberSuperId}`;
        memberUpId.show();

        console.log("✅ Updated #memberUpId with custom fields.");
    } catch (error) {
        console.error("❌ Error fetching contact details:", error);
    }
});

:small_blue_diamond: What This Code Does

  1. Fetches the logged-in member using currentMember.getMember().
  2. Gets the contactId (since custom fields are stored in Wix CRM, not Wix Members).
  3. Retrieves the contact’s custom fields (Schools and Member Super ID) using getContactById().
  4. Displays the custom field data in #memberUpId.

:warning: Important Notes:

getContactById is a backend function, so it must be placed in backend.jsw and then imported into your page.
Ensure your Contacts Database has the correct permissions set to allow this retrieval.

1 Like

(post deleted by author)

Thank you for the reply
I managed to get the CRM backend API working.
But I still no able to get the value.

I add this code to backend.jsw

import { getContactById } from 'wix-crm-backend';

export async function fetchMemberDetails(contactId) {
    try {
        if (!contactId) {
            throw new Error("No contact ID provided");
        }

        let contact = await getContactById(contactId);
        if (!contact) {
            throw new Error("Contact not found");
        }

        return {
            schools: contact.customFields["Custom School"] || "No School Info",
            memberSuperId: contact.customFields["Member UP ID"] || "No Super ID"
        };
    } catch (error) {
        console.error("❌ Error fetching member details:", error);
        return { schools: "Error fetching data", memberSuperId: "Error fetching data" };
    }
}

And this one on My Account Page

import { currentMember } from 'wix-members-frontend';
import { fetchMemberDetails } from 'backend/backend'; // Import the backend function

$w.onReady(async function () {
    let memberUpId = $w("#memberUpId");   // For Member UP ID
    let memberUpId2 = $w("#memberUpId2"); // For Custom School

    if (!memberUpId || !memberUpId2) {
        console.error("❌ Error: One or more elements not found.");
        return;
    }

    try { 
        // Get the currently logged-in member
        let member = await currentMember.getMember();

        if (!member) {
            console.error("❌ Error: No member is logged in.");
            return;
        }

        const contactId = member.contactId; // Get the contact ID
        console.log("✅ Contact ID:", contactId);

        // Call the backend function to get custom fields
        let { schools, memberSuperId } = await fetchMemberDetails(contactId);

        // Update the elements with the retrieved data
        memberUpId.text = `${memberSuperId}`;  // Member UP ID
        memberUpId2.text = `${schools}`;       // Custom School

        memberUpId.show();
        memberUpId2.show();

        console.log("✅ Updated #memberUpId and #memberUpId2 with custom fields.");
    } catch (error) {
        console.error("❌ Error fetching member details:", error);
    }
});


This is very frustrating. I don’t know if the field name is incorrect or if the contacts database does not have the correct permissions, which is causing the error below.

Error fetching member details: Cannot read properties of undefined (reading ‘member-up-id’)
Error fetching member details: Cannot read properties of undefined (reading ‘Member UP ID’)

How should I check for the correct permissions? I am using my owner account to work on this. Should I already have all the permissions?

Or the custom field key is not correct?

Thank you

I use dataset to display the value as an alternative method.

Here in the account profile stting. It there a way to disable / hide the field custom.member-up-id via velo code?
then it would be resolve my whole task…

Thank you