Search.../

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
badgeId
string

ID of the badge to assign the members to.

memberIds
Array<string>

IDs of the members to assign to the badge.

Returns

Fulfilled - IDs of the members assigned to the badge. Rejected - Error message.

Return Type:

Promise<Array<string>>

Was this helpful?

Assign a badge to members

Copy Code
1import { badges } from 'wix-members-backend';
2
3export 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 ];
10
11 return badges.assignMembers(badgeId, memberIds)
12 .then((assignedMembers) => {
13 return assignedMembers;
14 })
15 .catch((error) => {
16 console.error(error);
17 });
18}
19
20/* 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 */
Assign a badge to forum members with a top post

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.

Copy Code
1/*****************************
2 * Backend code - badges.jsw *
3 *****************************/
4
5import { badges } from 'wix-members-backend';
6
7export function assignMembersBackendFunction(badgeId, memberIds) {
8 return badges.assignMembers(badgeId, memberIds);
9}
10
11/*************
12 * Page code *
13 ************/
14
15import wixData from 'wix-data';
16import { assignMembersBackendFunction } from 'backend/badges';
17
18// ...
19
20// Get the "Top Poster" badge ID from the Members/Badges collection
21// Must be placed in an async function
22const badges = await wixData.query("Members/Badges")
23 .contains("title", "Top Poster")
24 .find();
25const topPosterBadgeId = badges.items[0]._id;
26
27wixData.query("Forum/Posts")
28 .gt("likeCount", 50)
29 .find()
30 .then((results) => {
31 if (results.items.length > 0) {
32 const topPosts = results.items;
33
34 // Assign post owner IDs to the topPostOwners array
35 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