Duplicate filter works in Preview mode but not in Live site

Hi there,

this is the code I use to prevent any double entry in my contest form, the problem is that it works in the Editor Preview mode and not in the live site…

I did sync the database from Live to SandBox, then SandBox to Live many times but still the same issue…

Contest form page:

$w.onReady(function () {
$w("#submitDataset").onAfterSave(() => {
// after the save is completed on the submitDataset,
// refresh the displayDataset which will refresh the connected repeater
$w('#displayDataset').refresh();
});
});

Backend - Data.js

import wixData from 'wix-data';
export function searchForDuplicates(value, info) {
// info contains the hook context
// use the collectionName property to use function for multiple collections
return wixData.query("Lead_Collections_DB")
.eq("email", value.email)
.find()
.then((results) => {
return results.items.length;
})
.catch((err) => {
let errorMsg = err;
});
}
export function Lead_Collections_DB_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(item, context).then((res) => {
if(res > 0) {
return Promise.reject('This item already exists');
}
return item;
});
}

Duplicate successful block in Preview mode:

Duplicate fail block in Live mode:


So what did I did wrong here, please?

Thanks
Yassine

It looks like you are using the eliminate duplicate values in a dataset tutorial.
https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-no-database-duplicates

Note that this works all fine as it is, the only thing that you needed to change was the name of your dataset for the beforeInsert function and the field key in the searchForDuplicates function in the backend data.js file

I have just tested this tutorial in preview mode and in live site mode and it all works fine as it is.

Also, make sure that your submitDataset and displayDataset have the same permissions setup as those in the tutorial itself.

Page Code
Nothing needs to be changed here just as long as your two datasets on this page have the element id names of submitDataset and displayDataset.

$w.onReady(function () {
$w("#submitDataset").onAfterSave(() => {
// after the save is completed on the submitDataset,
// refresh the displayDataset which will refresh the connected repeater
$w('#displayDataset').refresh();
});
});

Backend Data.js file
You only need to change the lines in bold and blue, otherwise everything else should stay the same.

export function noDuplicatesDB_beforeInsert(item, context) {
Make sure that noDuplicatesDB is changed to the dataset name in your Database folder.

return searchForDuplicates(context.collectionName, “names”, item).then((res) => {
Make sure that you change names to what your field key should be.

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;
});
}

Please note also that this tutorial is only setup for the single user input field, if you are wanting to have multiple user inputs to check, then have a look at using the code example from page 2 of the tutorial page and adjust it to suit your own website.
https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-no-database-duplicates/p-2

If you are still stuck with it, then note that Nayeli (Code Queen) has an excellent tutorial that you can view which should help you do it for a form with multiple inputs like yours.
https://codequeen.wixsite.com/membership-dashboard
https://www.youtube.com/watch?v=yLCOqsVHhD0&feature=youtu.be

Ok, I did follow the steps, but I still have the same issue, works in Editor but not Live…

You told me to check the permissions, in the example, it’s set to anyone in all the fields, here is mine, could it be because I set to admin that it’s not working in live? also, could it be dangerous to set to everyone?


I did also changed the code:

import wixData from 'wix-data';
export function searchForDuplicates(value, info) {
// info contains the hook context
// use the collectionName property to use function for multiple collections
return wixData.query("Lead_Collections_DB")
.eq("Email", value.Email)
.find()
.then((results) => {
return results.items.length;
})
.catch((err) => {
let errorMsg = err;
});
}
export function submitDataset_beforeUpdate(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;
});
}
export function submitDataset_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;
});
}

Set the permissions to be exactly as the tutorial permissions.

Note that in preview mode you are working with yourself as Admin role and so as the permissions are set to Admin only, of course it will work with yourself.

Whereas when you test it on a live site, you are not using it with yourself in the Admin role, you are just using it with yourself as a general user and therefore it won’t work as you are not Admin.

You need it set to Anyone on your live site otherwise nobody will be able to read anything from that collection apart from Admin which will be why it is not working for you.

Have a read here for more info.
https://support.wix.com/en/article/working-with-dataset-modes-and-collection-permissions
https://support.wix.com/en/article/about-collection-permissions#collection-permission-presets