Custom signup form error catching

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!

Update: I’ve managed to fix the refresh part, just had to add another .then after register. Now I’m working on solving the SearchUser function.

Update 2: After fixing the refresh issue, the error that displays -19976 is not there anymore. So that means that error was for the refresh part. Now there are 2 new errors saying when there’s an existing email. Something along the lines of “Error 500: Server error.”

Update 3: Testing with the SearchUser function alone, I’ve found that the ‘undefined’ log is for that function. The result for that log is always undefined regardless of whether or not the email already exists.

Update 4: I’ve moved the code from backend to front end. I now know that the log displays undefined because the current user isn’t logged in. That means they don’t have access to the “private” database. So I tried changing the code to searching for an existing email, and return the items.length, as well as logging in and refreshing the page, hoping the log would return 1, but it returned 0 instead. In preview mode it actually returns 1. So now I’m just confused…

bump

bumpies, please help me…at this point nothing makes sense anymore…

Ok, nevermind, it would seem that I’ve managed to solve this on my own. Basically, the permission settings only allows logged in members to view data from PrivateMembersData. Currently there’s no way to change this setting, so the only work around I’ve got is on the register page, allow the user to submit their registered email to a separate database. This database will be a duplicate (which is a shame) that the verify function will then refer to, whenever a new user registers. The reason why I don’t just use the duplicate instead of having both is because of the unique-ID-giving function that I don’t know how to create, as well as other benefits of having members area and stuffs (which again, is a shame…).