I would just like to prevent subscribers from signing up multiple times. I’ve read the links and I have the code. What I am looking for is an explanation of WHAT to change in the code. I have a simple subscription form. Only asks for email, nothing else. This is the code I copied from the example form. What do I change to prevent people from adding their email multiple times? Do I change something on the export function line and then the return line? Help please? my site is www dot stillfolding dot com
import wixData from ‘wix-data’ ;
export function searchForDuplicates(collection, field, item) {
// info contains the hook context
// use the collectionName property to use function for multiple collections
return wixData.query(collection)
.eq(field, item[field])
.find()
.then((results) => {
return results.items.length;
})
. catch ((err) => {
let errorMsg = err;
});
}
export function noDuplicatesDB_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, “names” , item).then((res) => {
if (res > 0 ) {
return Promise.reject( ‘This item already exists’ );
}
return item;
});
}
import wixData from 'wix-data';
$w.onReady(async function () {
// ----------------------------------------- User-Interface ------------------------------------------------
let collection = "YourCollectionNameHere"
let field = "ColumnInWhichToSearchHere"
let item = $w('#ConnectedInputFieldHere').value
// ----------------------------------------- User-Interface ------------------------------------------------
let RESULT = await searchForDuplicates(collection, field, item)
console.log(RESULT)
})
export function searchForDuplicates(collection, field, item) {
return wixData.query(collection)
.eq(field, item[field])
.find()
.then((results) => {
return results.items.length;
})
.catch((err) => {
let errorMsg = err;
});
}
export function noDuplicatesDB_beforeInsert(item, context) {
return searchForDuplicates(context.collectionName, "names", item).then((res) => {
if(res > 0) {
return Promise.reject('This item already exists');
}
return item;
});
}
Code-Improvement…
import wixData from 'wix-data';
// ----------------------------------------- User-Interface ------------------------------------------------
const buttonID = "HereTheIDofYourSUBMIT_BUTTON"
const Collection = "YourCollectionNameHere"
const Field = "ColumnInWhichToSearchHere"
var Item = $w('#ConnectedInputField_ID_Here').value //<----- Email-Input-Field
// ----------------------------------------- User-Interface ------------------------------------------------
$w.onReady(function () {
$w('#'+buttonID).onClick(async()=>{
let RESULT = await searchForDuplicates(Collection, Field, Item)
console.log(RESULT)
})
})
export function searchForDuplicates(collection, field, item) {
return wixData.query(collection)
.eq(field, item[field])
.find()
.then((results) => {
return results.items.length;
})
.catch((err) => {
let errorMsg = err;
});
}
export function noDuplicatesDB_beforeInsert(item, context) {
return searchForDuplicates(context.collectionName, "names", item).then((res) => {
if(res > 0) {
return Promise.reject('This item already exists');
}
return item;
});
}
Connect the USER-INTERFACE with your FORM!
const buttonID = "HereTheIDofYourSUBMIT_BUTTON"
const Collection = "YourCollectionNameHere"
const Field = "ColumnInWhichToSearchHere"
var Item = $w('#ConnectedInputField_ID_Here').value
Hi there! Where do we copy this code on the form?