Duplicate Site Members - Custom Registration

Hello,

I am looking to resolve an issue I am having where I created a custom registration sign up - but duplicate site members are being inserted into my “MemberDesignProfile” collection . The first time i click Sign up it does not catch the existing email, but it does catch it the second time i hit Sign Up.

Can someone help me correct my code so that any existing site members are found and not inserted into my member collection? Thank you!

Process flow of code:

  1. check for any validation errors (missing inputs)
  2. check for existing/matching email against “MemberDesignProfile” collection
    • IF - email match or validation errors - show error message
    • ELSE - all information is valid - THEN
      —> REGISTER USER as site member
      —> INSER USER RECORD in collection “Member Design Profile”

Code:
import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;

$w.onReady( () => {
$w.onReady( function () {
$w(“#signupNow”).onClick((event, $w) => {

//Validation errors if found//
let MissingFirst = ($w(“#registerFirstName”).valid === false );
let MissingLast = ($w(“#registerLastName”).valid === false );
let MissingEmail = ($w(“#registerEmail”).valid === false );
let MissingPassword = ($w(“#registerPassword”).valid === false );

//QUERY FOR EXISTING EMAIL IN COLLECTION - do not register or insert if found//
let ExistingEmailQUERY = wixData.query(“MemberDesignProfile”).eq(“emailAddress”, $w(‘#registerEmail’).value).find().then((results) => {
if (results.length > 0) {
$w(‘#emailexists’).show()
console.log(“email exists!”)
} else {
console.log(“no match & everything filled out”)}
let ExistingEmail = (results.length > 0)

console.log(MissingFirst)
console.log(MissingLast)
console.log(MissingEmail)
console.log(MissingPassword)
console.log(ExistingEmail)

//Check for validation errors or existing email/user record//
if (MissingFirst || MissingLast || MissingEmail || MissingPassword || ExistingEmail) {
$w(‘#missinginfo’).show()

} else {
$w(‘#missinginfo’).hide()
//IF NO MATCH OR VALIDATION ISSUES - Register Member into collection//
wixUsers.register($w(‘#registerEmail’).value, $w(‘#registerPassword’).value, {
contactInfo: {
“firstName”: $w(‘#registerFirstName’).value,
“lastName”: $w(‘#registerLastName’).value,
“email”: $w(‘#registerEmail’).value,
“password”: $w(‘#registerPassword’).value
}
})
.then( (result) => {
$w(‘#success’).expand();
})
. catch ( (err) => {
let errorMsg = err;
console.log(err)
$w(‘#error’).expand();
});

//IF NO MATCH OR VALIDATION ISSUES - Insert Member into collection//
let toInsert = {
“title”: $w(‘#registerFirstName’).value,
“lastName”: $w(‘#registerLastName’).value,
“emailAddress”: $w(‘#registerEmail’).value,
“password”: $w(‘#registerPassword’).value
};

wixData.insert(“MemberDesignProfile”, toInsert)
.then( (results2) => {
let REGISTERmember = results2;
console.log(results2)
} )

. **catch** ( (err2) => { 

let errorMsg = err2;
console.log(err2)
} );
}
})
})
})
})

something like this…

import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;

$w.onReady( function () {
$w(“#signupNow”).onClick((event, $w) => {

//Validation errors if found//
let MissingFirst = ($w(“#registerFirstName”).valid === false );
let MissingLast = ($w(“#registerLastName”).valid === false );
let MissingEmail = ($w(“#registerEmail”).valid === false );
let MissingPassword = ($w(“#registerPassword”).valid === false );

console.log(MissingFirst)
console.log(MissingLast)
console.log(MissingEmail)
console.log(MissingPassword)

//QUERY FOR EXISTING EMAIL IN COLLECTION - do not register or insert if found//
wixData.query(“MemberDesignProfile”).eq(“emailAddress”, $w(‘#registerEmail’).value).find().then((results) => {

if (results.length > 0){
$w(‘#emailexists’).show()
console.log(“email exists!”)
}

if ((results.length > 0) ||
(MissingFirst || MissingLast || MissingEmail || MissingPassword))
{

    console.log("not registered :( ") 
}  **else**  { 
  console.log("no match & everything filled out") 

//IF NO MATCH OR VALIDATION ISSUES - Register Member into collection//
wixUsers.register($w(‘#registerEmail’).value, $w(‘#registerPassword’).value, {
contactInfo: {
“firstName”: $w(‘#registerFirstName’).value,
“lastName”: $w(‘#registerLastName’).value,
“email”: $w(‘#registerEmail’).value,
“password”: $w(‘#registerPassword’).value
}
})
.then( (result) => {
$w(‘#success’).expand();
})
. catch ( (err) => {
let errorMsg = err;
console.log(err)
$w(‘#error’).expand();
});

//IF NO MATCH OR VALIDATION ISSUES - Insert Member into collection//
let toInsert = {
“title”: $w(‘#registerFirstName’).value,
“lastName”: $w(‘#registerLastName’).value,
“emailAddress”: $w(‘#registerEmail’).value,
“password”: $w(‘#registerPassword’).value
};

wixData.insert(“MemberDesignProfile”, toInsert)
.then( (results2) => {
let REGISTERmember = results2;
console.log(results2)
} )

. **catch** ( (err2) => { 

let errorMsg = err2;
console.log(err2)
} );

  } 

//Check for validation errors or existing email/user record//
if (MissingFirst || MissingLast || MissingEmail || MissingPassword) {
$w(‘#missinginfo’).show()

} else {
$w(‘#missinginfo’).hide()

}
})
})
})

@mikemoynihan99 Mike - Thank you so much it works great! I couldn’t figure out those nested If Statements - this was a huge help… :)!!

The only other problem with my original code is that my query to check for an existing email is a query on the COLLECTION with an “equals” .eq query on the email input. This query is case sensitive.

So the below duplicate scenario still happens - any suggestions? Thank you for all of your help!

First Sign Up: tarra.maxwell@email.com = successful REGISTER and INSERT TO COLLECTION

Second Sign Up: TARRA.MAXWELL@email.com = REJECTED REGISTRATION but still INSERTS TO COLLECTION AS NEW RECORD

Would there be a way to only insert the record into the collection IF registration is successful? I can’t figure out that nested if. It may simplify the code too…

appreciate any tips!

@tarrahopwood You need to make sure that you convert all emails to lowercase before you pass them to the database.

.toLowerCase

@mikemoynihan99 Can’t thank you enough! That’s what i figured! Just helps to run it by someone else - thank you so much!