I have a no duplicates function that i use for a user input, I would like to add another for the second user input on that screen.
import wixData from ‘wix-data’;
export function searchForDuplicates(noDuplicatesDB, field, item) {
// info contains the hook context
// use the collectionName property to use function for multiple collections
return wixData.query(noDuplicatesDB)
.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, “title”, item).then((res) => {
if (res > 0) {
return Promise.reject(‘This item already exists’);
}
return item;
})
}
How do I put this code in the above? I need no duplicates for title and name user inputs
return searchForDuplicates(context.collectionName, “name”, item).then((res) => {
if (res > 0) {
return Promise.reject(‘This item already exists’);
}
return item;