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"
* ]
* }
*/
Here you can see an example function to assign a badge for a member or for several members at once, depending on how much members (member-IDs) you put into the ARRAY.
Paste this function onto your BACKEND and send YOUR WISHED → ARRAY-DATA …
memberIds = [
"efab296e-2687-4751-9956-ee73200dd4bb",
"3403e13b-8826-4af6-aa19-18784bb84a8e",
"28d35f86-6694-4455-9dff-aff5d450b482",
];
…from your FRONTEND to your backend, starting the ‘myAssignMembersFunction()’ on your backend like…
myAssignMembersFunction(memberIds) ;
…or… like …
myAssignMembersFunction([
"efab296e-2687-4751-9956-ee73200dd4bb",
"3403e13b-8826-4af6-aa19-18784bb84a8e",
"28d35f86-6694-4455-9dff-aff5d450b482",
]);
All the IDs which you will have sent to the backend, will be assigned to a specific badge.
In this example it will be …
const badgeId = "3fcaacc0-a3a7-464f-9ba9-f211bdcec9fc";
Which is a fixed (already integrated) BADGE-ID within the export-function on your backend. But since you will want to send the specific BADEG-ID also from your frontend to your backend, you will need to edit/optimmize your backend-function, which will then recieve the BADGE-ID also from your frontend.
So your optimized function will look like…
export const myAssignMembersFunction = webMethod(Permissions.Anyone, (badgeId, memberIds) => {
return badges
.assignMembers(badgeId, memberIds)
.then((assignedMembers) => {
return assignedMembers;
}).catch((error) => {console.error(error);});
});
So in this case you can send your wished BADGE-ID and your wished MEMBER-IDs to your backend, to assign the choosen badge to your choosen members, calling it like…
myAssignMembersFunction(badgeId, memberIds)
BY THE WAY: Try to use the integrated option of CODE-BLOCKS instead of using pictures of your CODE.