Hi, I would like to stop duplicate entries using hook. Although, this code is working perfectly but I’m trying to take the result to front-end.
Is this possible to show error message on front-end?
If there is any improvement in code is possible then suggestions will be appreciated.
//data.js
import wixData from 'wix-data';
export function duplicates (collection, field, item){
return wixData.query(collection).eq(field, item[field])
.find()
.then((results)=>{
return results.items.length
})
.catch((err) => {
let errorMsg = err;
console.log(errorMsg);
});
}
export function email_beforeInsert(item, context) {
console.log(item);
console.log(context);
return duplicates(context.collectionName, "title", item)
.then((res)=>{
if (res > 0){
return Promise.reject(("this item exist"))
}
return item
})
}
export function phonebook_onFailure(error, context) {
let hookError = error;
return hookError
}