Among the biggest absurdities that I came across on trying to build this site is the following:
I have managed to prevent duplicate registrations by matching same email usage on custom registration form in sandbox DB collection (as in Preview mode) yet when the site goes published a user using the same email can (of course register once) yet submit to the collection I am using to collect their contact info without prevention.
This is crazy as it should have been the case if the code was properly being executed not allow a user to submit anything into the collection in the first place as the email which is trying to use to register already exists.
On the contrary not only manages to submit but messes up everything else such us user id, assigned account number, etc.
The code from backend:
//data.js
import wixData from 'wix-data';
export function searchForDuplicates(value) {
// info contains the hook context
// use the collectionName property to use function for multiple collections
return wixData.query("Members")
.eq("email", value.email)
.find()
.then((results) => {
return results.items.length;
})
.catch((err) => {
let errorMsg = err;
});
}
export function Members_beforeInsert(item, context) {
// Calls routine to check if value already exists in the collection.
// If value not found, then save record.
// Else, if value is found, then reject this insert to prevent duplicate values.
// Note: concurrent inserts may result in duplicates.
// Pass context to searchForDuplicates to use for multiple collections.
return searchForDuplicates(context.collectionName, "email", item).then((res) => {
if(res > 0) {
return Promise.reject('This item already exists');
}
return item;
});
}
The code on site:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(function () {
var min = 10000;
var max = 99999;
var bar = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(bar);
$w('#fxraccountnumber').value = "FXR" + String(bar);
$w('#submit').onClick( () => {
let email = [];
let fxraccountnumber = [];
email.push($w('#email').value);
// register as member using form data
wixUsers.register($w('#email').value, $w('#password').value, {
"contactInfo": {
"firstName": $w('#name').value,
"lastName": $w('#lastname').value,
"email": email,
"fxraccountnumber": $w('#fxraccountnumber').value
}
});
});
});
I have followed https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-no-database-duplicates and every other available article out there without success.
I have used all available information from API, Tutorials videos, etc. - so no need to respond with a link to tutorials (don’t waste your time please). If you have something valuable to add in code/suggestions it would be very much appreciated.
Thank you in advance for your time.