updateSlug( )
Changes the currently logged-in member's slug.
Description
The updateSlug()
function returns a Promise that resolves to a member
after the member's slug is changed.
Note: The APIs in
CurrentMember
are only partially functional when previewing your site. View a published version of your site to see their complete functionality.
Syntax
function updateSlug(slug: string): Promise<Member>
updateSlug Parameters
NAME
TYPE
DESCRIPTION
New slug.
Returns
Fulfilled - Updated member.
Return Type:
NAME
TYPE
DESCRIPTION
Member ID.
Email used by the member to log in to the site.
Member site access status.
One of:
"PENDING"
: Member created and is waiting for approval by site owner."APPROVED"
: Member can log in to the site."OFFLINE"
: Member is a guest author for the site blog and cannot log in to the site."BLOCKED"
: Member is blocked and cannot log in to the site."UNKNOWN"
: Insufficient permissions to get the status.
Contact ID.
Member privacy status.
One of:
"PUBLIC"
: Member is visible to everyone."PRIVATE"
: Member is hidden from site visitors and other site members. Member is returned only to site contributors and apps with the appropriate permissions."UNKNOWN"
: Insufficient permissions to get the status.
Member activity status.
One of:
"ACTIVE"
: Member can write forum posts and blog comments."MUTED"
: Member cannot write forum posts or blog comments."UNKNOWN"
: Insufficient permissions to get the status.
Date and time when the member was created.
Date and time when the member was updated.
Date and time when the member last logged in to the site.
Member's contact information. Contact information is stored in the Contact List.
Profile display info.
Was this helpful?
1import { currentMember } from 'wix-members-backend';23// Sample slug value:4// 'claude-morales-new-slug'56export const myUpdateSlugFunction = webMethod(Permissions.Anyone, (slug) => {7 return currentMember.updateSlug(slug)8 .then((updatedMember) => {9 const updatedSlug = updatedMember.profile.slug;10 const memberProfilePage = `https://yoursite.com/profile/${updatedSlug}/profile`;1112 return updatedMember;13 })14 .catch((error) => {15 console.error(error);16 });17}1819/* Promise resolves to:20 * {21 * "_id": "f32cbc51-a331-442b-86c2-2c664613e8b9",22 * "_createdDate": "2021-08-02T23:14:42.000Z",23 * "_updatedDate": "2021-08-02T23:14:58.345Z",24 * "loginEmail": "claude.morales@example.com",25 * "status": "APPROVED",26 * "contactId": "f32cbc51-a331-442b-86c2-2c664613e8b9",27 * "privacyStatus": "PUBLIC",28 * "activityStatus": "ACTIVE",29 * "lastLoginDate": "2021-08-04T05:10:21.000Z",30 * "contactDetails": {31 * "firstName": "Claude",32 * "lastName": "Morales",33 * "phones": [34 * "0747-769-460"35 * ],36 * "emails": [37 * "claude.morales@example.com"38 * ],39 * "addresses": [40 * {41 * "_id": "f0f4d905-488d-44db-9080-fc29078cfad5",42 * "addressLine": "9373 Park Avenue",43 * "addressLine2": "Berkshire",44 * "city": "Ely",45 * "subdivision": "GB-ENG",46 * "country": "GB",47 * "postalCode": "PD50 8EU"48 * },49 * {50 * "country": "IL"51 * }52 * ],53 * "customFields": {}54 * },55 * "profile": {56 * "nickname": "Claude Morales",57 * "slug": "claude-morales-new-slug"58 * }59 * }60 */
You can allow the currently logged-in member to change their slug
by providing a text box (in this example, #newSlug
)
and passing the value to a backend function that calls updateSlug()
.
This example also includes backend validation, which enforces a limit on the characters the slug can include. This allows you to apply the same validation to any number of inputs across your site.
1/*************************************2 * Backend code - change-slug.web.js *3 ************************************/45import { currentMember } from 'wix-members-backend';67export async function changeMemberSlug(slug) {8 // Validate the slug passed to this function.9 // This rule enforces that the slug can contain only lowercase letters (a-z),10 // numbers (0-9), hyphens (-), underscores (_), and pluses (+).11 // If the test passes, isValid is set to true.12 const comparison = /^[a-z0-9-_+]+$/m;13 const isValid = comparison.test(slug);1415 if (isValid === true) {1617 const updatedMember = await currentMember.updateSlug(slug);18 return {19 result: 'updated',20 member: updatedMember21 };2223 } else {2425 return {26 result: 'not_updated',27 reason: 'Invalid slug. Slug can contain only a-z, 0-9, -, _, or +'28 };2930 }31}323334/*************35 * Page code *36 ************/3738import { changeMemberSlug } from 'backend/change-slug';3940// ...4142$w('#updateSlugButton').onClick(async () => {43 console.log('Updating slug...');4445 const newSlug = $w('#newSlug').value;4647 // Pass the new slug to the backend changeMemberSlug()48 const updateSlugResponse = await changeMemberSlug(newSlug);4950 if (updateSlugResponse.result === 'updated') {5152 const updatedSlug = updateSlugResponse.member.profile.slug;53 const memberProfilePage = `https://yoursite.com/profile/${updatedSlug}/profile`;54 console.log('Slug updated. New member profile page:', memberProfilePage);5556 } else {5758 console.log('Slug not updated. Response:', updateSlugResponse);59 // Handle case where slug is not updated6061 }62});