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 Customer_Info_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, “e-mail”, item).then((res) => {
if (res > 0) { return Promise.reject(‘This item already exists’);
} return item;
});
}
The code works in preview mode not adding duplicates to the Customer_Info database but fails in the published version. So what do I do wrong.
I very much appreciate any help.
Thanks
Hi Yisreal, thank you for your reply. Yes I sync the data but still no success. It will work on preview returning the correct msg and not inserting into sandbox data but on the published version it does insert duplicates. So I do miss something but don’t know what.
Thanks again
@yisrael-wix
so now I checked the permissions given on the example. This gives permission on everything to anyone. Doing this on my database nothing gets inserted into the live database and fails with “user already exists” , even if not. So I changed some back and now the submit works o.k again on the sandboxed data but again fails on the live data. Bummer
@gerhardlowe9 Checking out Live sites is a bit more difficult for us to check since it involves the published site, but I’ll give it a try and maybe I’ll see something. Please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor.Please include the name of the page involved.
@yisrael-wix
Thank you for your trouble. The url for the website is:
and the name of the page in question is sign up. The page is hidden and will be called after the customer is reading the eula and then proceeding to download. I have also installed the the vanilla sign up as I didn’t get anywhere. So I can bypass the custom signup.
Again thank you,
I suspect that the query didn’t get any results, and therefore returned an error (which probably ends up as > 0.
You could change the permission to Anyone . A better (more secure) solution would be to change the find() in your query to suppressAuthentication - backend code can get away with that.
.find({ "suppressAuth":true })
I hope this helps. I wasn’t able to test it since I’m not going to publish the site.
Thanks for the help again. Now I am back to 3 days ago situation whee I can enter the first email to an empty database but nothing more. O.K for the time being I shall bypass my custom login and go with the vanilla login bar to play around with diffrent code examples. Thanks again.