Struggling with wix-auth elevate() for createMember( )

I’m using Wix Studio and have run into a problem trying to use createMember( ) from wix-members.V2 in the API, which I am just using to get the site members set up, when I take it live.

I have a contact that I am trying to change into member. The API clearly says “This function requires elevated permissions to run”. So I have been trying to incorporate the elevate() function into my code.

The frontend code looks like this:

import { getContacts } from 'backend/contactFunctions.web.js';
import { contactToMember } from 'backend/contactFunctions.web.js';

$w.onReady(async function () {
    let allContacts = await getContacts();
    const targetEmail = "xxxxxx@xxxxxxxxxx.com"; (removed)

    const filteredContact = allContacts.find(contact =>
        contact.info.emails && contact.info.emails.some(email => email.email === targetEmail)
    );
	console.log(filteredContact)

    if (filteredContact) {
        try {
            let newMem = await contactToMember(filteredContact); 
            console.log(newMem);
        } catch (error) {
            console.error("Error creating new member from frontend:", error);
            // Handle error appropriately in the UI, e.g., show an error message to the user
        }
    } else {
        console.log("Contact not found.");
    }
});

The first portion runs fine and I get the filteredContact back from the contacts query, which I haven’t shared here (as it is working fine.

My backend code looks like this:

import { members } from 'wix-members.v2';
import * as wixAuth from 'wix-auth';

// Backend code to make a contact a member
export const contactToMember = webMethod(Permissions.Anyone, async (contact) => {
    let logInEmail
    console.log("contactToMember running...");
    const elevatedCreateMember = wixAuth.elevate(members.createMember);
    const contactRestructured = {
        firstName: contact?.info?.name?.first || "",
        lastName: contact?.info?.name?.last || "",
        emails: contact?.info?.emails || [],
        phones: contact?.info?.phones || [],
        customFields: contact?.info?.extendedFields || {}
    };
    try {
        if (contact.info.emails) {
            logInEmail = contact.info.emails[0].email
        }
        console.log("logInEmail: "+logInEmail)
        let member = {
            contact: contactRestructured,
            loginEmail: logInEmail,
            //privacyStatus: "PUBLIC"
        };
        const result = await elevatedCreateMember(member); // Call the elevated function
        return result; // Return the successful result
    } catch (error) {
        console.error("Error in elevatedCreateMember:", error);
        throw error; // Re-throw the error to be caught by the caller
    }
})

Progress dies as soon as the back-end fires:

Error creating new member from frontend: Error: message: Unexpected value for StringValue
details:
   applicationError:
      description: Bad Request code: BAD_REQUEST
      contactToMember running...
logInEmail: xxxxxxx@xxxxxxxx.com (removed)
message: Unexpected value for StringValue
details:
   applicationError:
      description: Bad Request 
      code: BAD_REQUEST
      data:{}

When I run it ina browser window (ie not in Preview), I see this error instead:

Error creating new member from frontend: Error: Unable to handle the request. Contact the site administrator or view site monitoring logs for more information.

There isn’t any more detail in the site monitoring logs.

This is the object I am trying to create a member with, which seems in line with what the API wants (despite the name construction being different to Contacts).

{
  "firstName": "XXXXXX",
  "lastName": "YYYYYY",
  "emails": [
    {
      "tag": "UNTAGGED",
      "email": "xxxxxx@yyyyyy.com",
      "primary": true,
      "id": "fba28eed-68aa-4f5a-9215-022d38228bf2"
    }
  ],
  "phones": [],
  "customFields": {
    "custom.paymentmethod": "Cheque",
    "contacts.displayByLastName": "xxxxxx yyyyyy",
    "invoices.vatId": "",
    "emailSubscriptions.deliverabilityStatus": "NOT_SET",
    "custom.journaltype": "PDF",
    "emailSubscriptions.subscriptionStatus": "NOT_SET",
    "custom.lastpaymentdate": "2024-04-02",
    "custom.amountlastpaid": 10,
    "emailSubscriptions.effectiveEmail": "xxxxxx@yyyyyy.com",
    "custom.addr5statecounty": "Hampshire",
    "custom.joineddate": "2024-04-02",
    "contacts.displayByFirstName": "xxxxxx yyyyyy",
    "custom.addr7country": "UK",
    "custom.membernumber": 20000,
    "custom.renewaldate": "2025-04-01",
    "custom.addr6postcodezip": "SO220FD",
    "custom.paymentstatus": "Up to date",
    "custom.addr2": "Peters Rd",
    "custom.addr4city": "Romsey",
    "custom.addr-1-house-name": "Hogshead",
    "custom.title": "Mr",
    "custom.type": "Person"
  }
}

I suspect I am doing something dum with the elevated permissions, but I can’t see what. Can anybody help??

I think you have problem with the object you are sending to createMember API. Try to console log your last object (member) and see if it’s okay.

You can’t pass an object within emails array it should be an email as string. But if there isn’t a problem with that. I also don’t think there is something wrong with elevate function.

If I remove the memnber object this works fine and I can live with that. Thanks for the feedback.