Assigns a badge to site members.
The assignMembers()
function returns a Promise
that resolves when the specified badge is assigned to the specified members.
The badgeId
parameter must be an ID from your site's Members/Badges
collection.
Typically, you retrieve the ID from the collection using a query or through a dataset.
function assignMembers(
badgeId: string,
memberIds: Array<string>,
): Promise<Array<string>>;
ID of the badge to assign the members to.
IDs of the members to assign to the badge.
import { Permissions, webMethod } from "wix-web-module";
import { badges } from "wix-members-backend";
export const myAssignMembersFunction = webMethod(Permissions.Anyone, () => {
const badgeId = "3fcaacc0-a3a7-464f-9ba9-f211bdcec9fc";
const memberIds = [
"efab296e-2687-4751-9956-ee73200dd4bb",
"3403e13b-8826-4af6-aa19-18784bb84a8e",
"28d35f86-6694-4455-9dff-aff5d450b482",
];
return badges
.assignMembers(badgeId, memberIds)
.then((assignedMembers) => {
return assignedMembers;
})
.catch((error) => {
console.error(error);
});
});
/* Promise resolves to:
* {
* "memberIds": [
* "efab296e-2687-4751-9956-ee73200dd4bb",
* "3403e13b-8826-4af6-aa19-18784bb84a8e",
* "28d35f86-6694-4455-9dff-aff5d450b482"
* ]
* }
*/
Creates a badge.
The createBadge()
function returns a Promise
that resolves to the newly created badge.
New badges do not have any badge permissions by default. You can set badge permissions from the Badges page in the Dashboard.
If backgroundColor
or textColor
are not specified, they default to "#796EFF"
(purple) and "#FFFFFF"
(white) respectively.
function createBadge(badgeInfo: BadgeInfo): Promise<Badge>;
Settings for the new badge.
import { Permissions, webMethod } from "wix-web-module";
import { badges } from "wix-members-backend";
export const myCreateBadgeFunction = webMethod(Permissions.Anyone, () => {
const badgeInfo = {
title: "Rising Star",
description: "Contributed 5 posts this month",
backgroundColor: "#FED8B1",
textColor: "#000000",
icon: "https://static.wixstatic.com/shapes/132b9a3d51884000acaf705eeeb0e296.svg",
};
return badges
.createBadge(badgeInfo)
.then((badge) => {
const badgeTitle = badge.title;
const badgeSlug = badge.slug;
return badge;
})
.catch((error) => {
console.error(error);
});
});
/*
* Promise resolves to:
* {
* "_id": "571495e9-98af-4ec9-b854-16c0293c9312",
* "_createdDate": "2021-05-20T00:02:18.794Z",
* "_updatedDate": "2021-05-20T00:02:18.794Z",
* "title": "Rising Star",
* "slug": "rising-star",
* "description": "Contributed 5 posts this month",
* "backgroundColor": "#FED8B1",
* "textColor": "#000000",
* "icon": "https://static.wixstatic.com/shapes/132b9a3d51884000acaf705eeeb0e296.svg",
* "roleId": "571495e9-98af-4ec9-b854-16c0293c9312"
* }
*/
Deletes a badge.
The deleteBadge()
function returns a Promise that resolves when
the specified badge is deleted.
The badgeId
parameter must be an ID from your site's Members/Badges
collection.
Typically, you retrieve the ID from the collection using a query or through a dataset.
function deleteBadge(badgeId: string): Promise<void>;
ID of the badge to delete.
import { Permissions, webMethod } from "wix-web-module";
import { badges } from "wix-members-backend";
export const myDeleteBadgeFunction = webMethod(Permissions.Anyone, () => {
const badgeId = "95fad9e6-d4dc-4d84-98a2-ae8660cce599";
return badges
.deleteBadge(badgeId)
.then(() => {
console.log("Badge deleted");
})
.catch((error) => {
console.error(error);
});
});
Lists the badges assigned to each of the specified site members.
The listMemberBadges()
function returns a Promise that resolves to a
list of badge IDs associated with each of the specified members.
function listMemberBadges(
memberIds: Array<string>,
): Promise<Array<MemberBadges>>;
List of member IDs to list badges for.
import { Permissions, webMethod } from "wix-web-module";
import { badges } from "wix-members-backend";
export const myListMemberBadgesFunction = webMethod(Permissions.Anyone, () => {
const memberIds = [
"32cf071a-cc2f-450f-ad74-5a25db0b1b6a",
"2cb1846f-0c7a-4c39-8736-349236cfab40",
"69nh659a-ic2f-950f-ed74-14a25db9b1b6j",
];
return badges
.listMemberBadges(memberIds)
.then((memberBadges) => {
const firstMemberBadges = memberBadges[0].badgeIds;
return memberBadges;
})
.catch((error) => {
console.error(error);
});
});
/*
* Promise resolves to:
* [
* {
* "memberId": "32cf071a-cc2f-450f-ad74-5a25db0b1b6a",
* "badgeIds": [
* "0d37ea22-44b0-4a62-9818-ff660178c439",
* "c7d1a81d-485a-4eef-872f-4595ab2a15a2"
* ]
* },
* {
* "memberId": "2cb1846f-0c7a-4c39-8736-349236cfab40",
* "badgeIds": [
* "0d37ea22-44b0-4a62-9818-ff660178c439",
* "41dcda59-8b6d-4deb-bb1f-d283de044b85",
* "df9fc0e2-c2ba-40ba-a160-200a016c3507"
* ]
* },
* {
* "memberId": "69nh659a-ic2f-950f-ed74-14a25db9b1b6j",
* "badgeIds": [
* "df9fc0e2-c2ba-40ba-a160-200a016c3507"
* ]
* }
* ]
*/
Lists the IDs of all members assigned to a badge.
The listMembers()
function returns a Promise that resolves
to a list of member IDs assigned to the specified badge.
function listMembers(badgeId: string): Promise<Array<string>>;
ID of the badge to list members of.
import { Permissions, webMethod } from "wix-web-module";
import { badges } from "wix-members-backend";
export const myListMembersFunction = webMethod(Permissions.Anyone, () => {
const badgeId = "571495e9-98af-4ec9-b854-16c0293c9312";
return badges
.listMembers(badgeId)
.then((memberIds) => {
const firstMember = memberIds[0];
return memberIds;
})
.catch((error) => {
console.error(error);
});
});
/*
* Promise resolves to:
* [
* "28d35f86-6694-4455-9dff-aff5d450b482",
* "72751428-2743-4bda-acf5-4218a4279cd3",
* "8831eed6-928e-4f85-b80a-e1e48fb7c4fd"
* ]
*/
Removes site members from an assigned badge.
The removeMembers()
function returns a Promise that resolves
when the specified members are removed as holders of the specified badge.
The badgeId
parameter must be an ID from your site's Members/Badges
collection.
Typically, you retrieve the ID from the collection using a query or through a dataset.
function removeMembers(
badgeId: string,
memberIds: Array<string>,
): Promise<void>;
ID of the badge to remove the members from.
IDs of the members to remove from the badge.
import { Permissions, webMethod } from "wix-web-module";
import { badges } from "wix-members-backend";
export const myRemoveMembersFunction = webMethod(Permissions.Anyone, () => {
const badgeId = "3fcaacc0-a3a7-464f-9ba9-f211bdcec9fc";
const memberIds = [
"efab296e-2687-4751-9956-ee73200dd4bb",
"3403e13b-8826-4af6-aa19-18784bb84a8e",
"28d35f86-6694-4455-9dff-aff5d450b482",
];
return badges
.removeMembers(badgeId, memberIds)
.then(() => {
console.log("Members removed from badge");
})
.catch((error) => {
console.error(error);
});
});