Hello!
I’m trying to create a custom signup form for my website. So far everything’s gone well, except the error displaying part. Right now I’m making a code that will search for the PrivateMembersData database for the email the user’s entered. If there’s already 1 existing it will display an error, if not it will register then login at the same time.
Here’s what my code currently looks like:
Backend: (File name’s called privateDataSearch.jsw)
import wixData from ‘wix-data’;
export function SearchUser(email){
wixData.query(“Members/PrivateMembersData”)
.eq(“loginEmail”, email)
.find()
.then((results) => {
console.log(results.items.length);
if (results.items.length > 0) {
return true ;
}
})
}
Front end:
//codes for importing
…
import {SearchUser} from ‘backend/privateDataSearch’;
let firstName = $w(‘#userFirst’).value;
let lastName = $w(‘#userLast’).value;
let email = $w(‘#email’).value;
let password = $w(‘#pwd’).value;
SearchUser(email)
.then( function (exist) {
console.log(exist);
//If there’s already an email like that, display error
if (exist === true ) {
$w(“#err”).text = “Email already exists”;
$w(“#err”).show();
}
//If not, register the user and log in
else {
wixUsers.register(email, password, {
contactInfo: {
“firstName”: firstName,
“lastName”: lastName
}
})
. catch ((err) => {
console.log(err);
});
wixUsers.login(email, password)
.then(() => {
//Refresh the page
wixLocation.to(wixLocation.url);
})
. catch ((err) => {
console.log(err);
});
}
})
})
})
When I run the code on the website:
- If there isn’t an email already existing, I’d have to click the register button twice in order to trigger the wixLocation.to(wixLocation.url) (the first click did register and login the user, it just didn’t refresh the page, I had to manually press F5 to see the effect).
- If there is an email already existing, the “#err” text element wouldn’t show, the variable ‘exist’ would appear as undefined in the console log, along with an error saying -19976. I tried searching online to see what that means but I couldn’t find it…
Any help would be appreciated.
Thanks in advance!