Need Help with Custom Error Message when signing up new site members

Hi, I’m trying to add a custom error message that pops up when a new user tries registering with an email that already exists. I can get the error message to show up in Preview mode, but it does not seem to be working in live mode. Furthermore, the register/redirect does not seem to be working with unique emails either. Here’s the code I’m using:

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import wixData from 'wix-data';

$w.onReady(function () {
    $w('#registerNow').onClick(function () {
      let email = $w('#registerEmail').value;
      let password = $w('#registerPassword').value;
      // Check if member already exists
        wixData.query("Members").eq("emailAddress", email)
        .find()
        .then((results) => {
           if (results.items.length > 0) {
               $w('#errorMessage').show();
               console.log(results.items.length);
               return;
                }
            wixUsers.register(email, password)
            .then((result) => {
                 let userId = result.user.id;
                 let toSave = {
                     "_id": userId,
                        }
                      wixData.save('Members', toSave)
                          .then(() => {
                             wixLocation.to('/profile-creation')
                            })
                    })
            })
            .catch((err) => {
                console.log(err);
            });

    })
});

"Hi, I’m trying to add a custom error message that pops up when a new user tries registering with an email that already exists. I can get the error message to show up in Preview mode , "

  • —> in preview mode you have admin permissions, maybe check the members collection permissions? remember that when you go to the live site you are considered as a guest.

let me knot if it helped you,
gal.

Also it seems that you are trying to mash together a basic signup form that is like this here.

import wixUsers from 'wix-users';
import wixWindow from 'wix-window';
import wixLocation from 'wix-location';

$w.onReady(function () {
    
    $w("#registerButton").onClick( (event) => {
        
   let email = $w("#email").value;
   let password = $w("#password").value;
   let first = $w("#firstName").value;
   let last = $w("#lastName").value;

   wixUsers.register(email, password, {
       contactInfo: {
        "firstName": $w('#firstName').value,
        "lastName": $w('#lastName').value
       }
      } )
      .then( (result) => {
        let resultStatus = result.status;
  wixWindow.lightbox.close();
  wixLocation.to("/sign-in-status");  //Change the URL ending to whatever page you want to send the user to after they log in.
      } );     
    } );
    
});

Along with code taken from the Wix Members Profile tutorial as shown here.

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady( () => {
  if(wixUsers.currentUser.loggedIn) {
    $w("#loginButton").label = "Logout";
    $w("#profileButton").show();
  }
  else {
    $w("#loginButton").label = "Login";
    $w("#profileButton").hide();
  }
} );

export function loginButton_click(event) { 
  // user is logged in
  if(wixUsers.currentUser.loggedIn) {
    // log the user out
    wixUsers.logout()
      .then( () => {
        // update buttons accordingly
        $w("#loginButton").label = "Login";
        $w("#profileButton").hide();
    } );
  }
  // user is logged out
  else {
    let userId;
    let userEmail;
  
    // prompt the user to log in 
    wixUsers.promptLogin( {"mode": "login"} )
      .then( (user) => {
        userId = user.id;
        return user.getEmail();
      } )
      .then( (email) => {
        // check if there is an item for the user in the collection
        userEmail = email;
        return wixData.query("Members")
          .eq("_id", userId)
          .find();
      } )
      .then( (results) => {
        // if an item for the user is not found
        if (results.items.length === 0) {
          // create an item
          const toInsert = {
            "_id": userId,
            "email": userEmail
          };
          // add the item to the collection
          wixData.insert("Members", toInsert)
            .catch( (err) => {
              console.log(err);
            } );
        }
        // update buttons accordingly
        $w("#loginButton").label = "Logout";
        $w("#profileButton").show();
      } )
      .catch( (err) => {
        console.log(err);
      } );
  }
}

//rest of code....//

If you use the search function in this forum, you would have found suitable posts for this…
https://www.wix.com/corvid/forum/community-discussion/duplicate-site-members-custom-registration
https://www.wix.com/corvid/forum/community-discussion/prevent-contact-duplicates-in-custom-registration
https://www.wix.com/corvid/forum/community-discussion/prevent-contact-duplicates-in-custom-registration-1
https://www.wix.com/corvid/forum/community-discussion/prevent-contact-duplicates-in-custom-registration-without-costume-database