Example: No Database Duplicates

Blai, Since it’s not working for you, I would say that you don’t have “exactly the same”. Please post the URL of your site. Only authorized Wix personnel can get access to your site in the editor.

This was very helpful, thank you. The code worked great for me, and helped me figure out another issue I was struggling with. I do have one small question, though: How did you get the repeater to refresh after a successful submit? In my reproduction I only see the new items on page refresh. Thanks.

Hey Tim, good point. Try this on page:

$w.onReady(function () {
   $w("#submitDataset").onAfterSave(() => {
    console.log("After save");
    $w('#displayDataset').refresh();
  });
});

What this does is after the save is completed on the submitDataset, it refreshes the displayDataset forcing the Repeater to refresh.

Thanks Yisrael. That did the trick.

@Blai,

You don’t say what page you’re trying this on.

Also, you can’t use the wix-locations API in the backend code (your data.js file). It is only available in front-end code.

Okay! But in your example you don’t say which page do you want this to work.

I want to use it in the Company Update page. Its a dynamic page, maybe it should be a normal page? But I need that to redirect every user to their profile update…

Blai,

You don’t say what isn’t working. Also, your database is empty so you won’t have duplicates. You should review the code in the example and ensure that you are implementing your code correctly.

I would suggest carefully reviewing the information about Database Collections and Connecting Data . Also, refer to the information about Data hooks .

Good luck,

Yisrael

Because I deleted everything, I had like 50 users. Okay, let me try it again and I will show you, you will see it’s not working

What isn’t working? Does the data get entered? Does it find duplicates?

I tried now with the exactly same code and its working. Is it possible that it wasn’t working with all the data that I uploaded before adding the code? Cause now I deleted all the data and with the new data is working fine >.<

That’s the code (maybe it helps to other users)

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("Company")
        .eq("companyName", value.companyName)
        .find()
        .then((results) => {
 return results.items.length;
        })
        .catch((err) => {
 let errorMsg = err;
        })
}


export function searchForDuplicates2 (value, info) {
 // info contains the hook context
 // use the collectionName property to use function for multiple collections
 return wixData.query("TravelJobs")
        .eq("jobName", value.jobName)
        .find()
        .then((results) => {
 return results.items.length;
        })
        .catch((err) => {
 let errorMsg = err;
        })
}

export function Company_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(item, context).then((res) => {
 if(res > 0) {
 
 return Promise.reject('This item already exists');
        }
 return item;
    });
}

export function Jobs_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 searchForDuplicates2 (item, context).then((res) => {
 if(res > 0) {
 
 return Promise.reject('This item already exists');
        }
 return item;
    });
}


export function Jobs_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 searchForDuplicates2 (item, context).then((res) => {
 if(res > 0) {
 
 return Promise.reject('This item already exists');
        }
 return item;
    });
}


Thanks and sorry!

Hey Blai, maybe it’s magic. :upside_down_face: Glad you got it working and for sharing your code. :beers:

We’re Just Fixing A

Few Things!
We hope to be back online very soon.
Please try again in a few minutes. (Error 500) Still having problems? Contact Support

Been like that for a day at least. The contact support link - gives one the usual bums rush.

Do you have an updated link ?

@ross95874 Is it working for you now? I just tried it myself and everything seems OK.

@yisrael-wix I still get the issue if I use your link in the post. If I go to the top of the page where there seems to be summary - that link works - but not sure if it where you wanted us to go.

Hey @ross95874 - Man! I hate it when I’m stupid. I wasn’t paying attention to what link you were referring to. Just fixed it now and all is right with the world.

Thanks for pointing this out.

Assuming I am at the correct page I am seeing two lots of code. Do I need thme both.

  1. The first bit is on the home page. Is this needed - and if so what is the trigger ? The only button I can see is the Submit button but the example does not have any events tied to the button.
    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();
    });
    });
    I already have a create user and send email on my button. If I include this code will it cause issues ?

  2. The other bit of code I see is the file called data.js. This I have copied into my backend code page. I have highlighted the items I believe need to be customised. Can you confirm these are the items? Everything else stays the same ?
    Do you have a link to some article telling us how to bring this all alive ? I see nyeri has put up a utube link - might watch it as well

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

@ross95874 You will need all of the code in the example - that’s what makes it work. You can of course modify it for your specific requirements.

See the onAfterSave() API for information on what triggers it and how its used.

And, it’s always worth watching @code-queen 's videos.

Hi, Ysrael, I know this post was written some time ago, but I too tried to insert this code into my own site, and unsurprisingly, my debugger complains about the “#saveDataset” and ‘#dislplayDAtatset’. Also, I see that the comments #text5 and #text6 are attached to a NoDuplicatesDB submit dataset which I cannot see in your database (I only see the NoDuplicatesDB dataset.

Hi Yisrael, I am using the code that we have here as an example. However, when I test, I am still able to resubmit the same information in my form. It does not prevent me from registering twice. I am sure exactly I am missing.

As you mentioned in an earlier post, I would like to also share my URL, however you mentioned to Only authorized Wix personnel can get access to your site in the editor. How do I do that? And I am not sure about how I set up the repeater as well.

Here is what I am using: 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(“CRMEVENTS”)
.eq(“Email”, value.Email)
.find()
.then((results) => {
return results.items.length;
})
. catch ((err) => {
let errorMsg = err;
});
}

export function CRMEVENTS_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;
});
}

Without seeing what your elements or .js file name looks like … I am going to take a wild guess and assume that “Email” should be “email” (lower case E).

Also … feel free to watch my tutorial where I briefly explain this code & use it to create a membership website … Create Member Website with Different Types of Members - Build Profile Page - YouTube