Hello, I am trying to determine what I can do to prevent multiple members from saving the same nickname. Our member pages are using a Member Profile Card, and from what I can see it appears that the name a member can modify there by clicking “Edit Profile” is bound to the Nickname field of the Members/PrivateMembersData collection.
I have been reading about data hooks, and specifically the beforeInsert() hook . I have also tried following the No Database Duplicates Example . When I click on “Add/Remove Hooks” for a collection, I see that many of them offer a hook Before Insert. Unfortunately, this hook does not appear to be available for the PrivateMembersData collection.
I even tried to just name a beforeInsert function semantically, as follows, to see if it might still work, but so far it does not appear to:
import wixData from 'wix-data';
export function searchForDuplicates(collection, field, item) {
return wixData.query(collection)
.eq(field, item[field])
.find()
.then((results)=> {
return results.items.length;
});
.catch((err)=>{
let errorMsg = err;
console.log("Error searching for duplicates: " + errorMsg);
});
}
export function PrivateMembersData_beforeInsert(item, context) {
const collectionName = 'Members/PrivateMembersData';
//return searchForDuplicates(context.collectionName, "nickname",item).then((res)=> {
return searchForDuplicates(collectionName, "nickname",item).then((res)=> {
if (res > 0) {
return Promise.reject('This item already exists');
}
return item;
});
}
Is there any way to perform a validation check before writing to the Nickname field in PrivateMembersData? If the beforeInsert() hook is not available in this specific case, are there other things I can do that would force the named entered on the Member Profile Card to be unique to that specific user?