Prevent Replica User Names

Hi,

I have just watched one of Code Queen 's tutorials on how to prevent duplicate usernames. I copied everything to replicate her code replacing the areas required to match my site.

The database/collection’s name is PlayersProfile and the label for the User Name field is - Title.

It doesn’t


seem to work, can any one please advise?

I see 1 thing: .the((results) should be .then((results).

Is there any reason why you have not used the full code given for this data.js backend file?
https://codequeen.wixsite.com/membership-dashboard/the-backend-code

// Programmer: Nabeel

import wixData from 'wix-data';

export function searchForDuplicateUsernames(value) {
    return wixData.query("profile")
        .eq("username", value.username)
        .find()
        .then((results) => {
            return results.items.length;
        })
        .catch((err) => {
            let errorMsg = err;
        })
}
// emailAddress
 
export function searchForUsernamesIds(value) {
    //This will check if the username and email is the same i.e, The username is not changed by the user
    return wixData.query("profile")
        .eq("username", value.username)
        .eq("emailAddress" , value.emailAddress)
        .find()
        .then((results) => {
            return results.items.length;
        })
        .catch((err) => {
            let errorMsg = err;
        })
}
export function profile_beforeInsert(item, context) {
        return searchForDuplicateUsernames(item, context).then((res) => {
        if(res > 0) {
            return Promise.reject('This username is already taken');
        }
        return item;
    });
}
 
export function profile_beforeUpdate(item, context, value) {
    //courtesy of Salman from the Totally Codable Code community that helped finish this code
    //fb.com/salman2301 or  preferred facebook
    return searchForUsernamesIds(item, context).then((res) => {
        if(res > 0) {
            // the user name is not change but some other value is changed
            return Promise.resolve('Updated the changes');
        } else {
            // The username is changed , Checking if the username value is taken by some other member or not
            return searchForDuplicateUsernames(item, context).then((res) => {
                if(res > 0){
            return Promise.reject('This username is already taken');
                }
        })
        }
        return item;
    });
 
}
 
export function onUpDate(value) {
    return wixData.query("profile")
        .eq("username", value.username)
        .find()
        .then((results) => {
            return results.items.length;
        })
        .catch((err) => {
            let errorMsg = err;
        })
}