assignMembers( )
Assigns a badge to site members.
Description
The assignMembers()
function returns a Promise
that resolves when the specified badge is assigned to the specified members.
Note: This function replaces the deprecated
wix-users-backend.badges.assignMembers()
. The deprecated function will continue to work, but it will not receive updates. To keep any existing code compatible with future changes, see the migration instructions.
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.
Syntax
function assignMembers(badgeId: string, memberIds: Array<string>): Promise<Array<string>>
assignMembers Parameters
NAME
TYPE
DESCRIPTION
ID of the badge to assign the members to.
IDs of the members to assign to the badge.
Returns
Fulfilled - IDs of the members assigned to the badge. Rejected - Error message.
Return Type:
Was this helpful?
1import { badges } from 'wix-members-backend';23export function myAssignMembersFunction() {4 const badgeId = "3fcaacc0-a3a7-464f-9ba9-f211bdcec9fc";5 const memberIds = [6 "efab296e-2687-4751-9956-ee73200dd4bb",7 "3403e13b-8826-4af6-aa19-18784bb84a8e",8 "28d35f86-6694-4455-9dff-aff5d450b482"9 ];1011 return badges.assignMembers(badgeId, memberIds)12 .then((assignedMembers) => {13 return assignedMembers;14 })15 .catch((error) => {16 console.error(error);17 });18}1920/* Promise resolves to:21 * {22 * "memberIds": [23 * "efab296e-2687-4751-9956-ee73200dd4bb",24 * "3403e13b-8826-4af6-aa19-18784bb84a8e",25 * "28d35f86-6694-4455-9dff-aff5d450b482"26 * ]27 * }28 */
This example first queries the Members/Badges collection
and assigns the first badge to topPosterBadgeId
.
Then it queries the Forum/Posts collection
for all posts with a Like Count greater than 50.
The member IDs of the post owners are then stored in an array,
which is passed to the backend function
which assigns those members a "Top Poster" badge.
1/*****************************2 * Backend code - badges.jsw *3 *****************************/45import { badges } from 'wix-members-backend';67export function assignMembersBackendFunction(badgeId, memberIds) {8 return badges.assignMembers(badgeId, memberIds);9}1011/*************12 * Page code *13 ************/1415import wixData from 'wix-data';16import { assignMembersBackendFunction } from 'backend/badges';1718// ...1920// Get the "Top Poster" badge ID from the Members/Badges collection21// Must be placed in an async function22const badges = await wixData.query("Members/Badges")23 .contains("title", "Top Poster")24 .find();25const topPosterBadgeId = badges.items[0]._id;2627wixData.query("Forum/Posts")28 .gt("likeCount", 50)29 .find()30 .then((results) => {31 if (results.items.length > 0) {32 const topPosts = results.items;3334 // Assign post owner IDs to the topPostOwners array35 const topPostOwners = topPosts.map(post => post._ownerId);36 assignMembersBackendFunction(topPosterBadgeId, topPostOwners);37 } else {38 console.log("No top posts");39 }40 })41 .catch((error) => {42 console.log(error);43 });44