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() } }) }) })