# Example: No Database Duplicates

**URL:** <https://forum.wixstudio.com/t/example-no-database-duplicates/33085>\
**Category:** Show and tell\
**Tags:** tips, code\
**Created:** [June 27, 2018, 11:53am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085 "2018-06-27T11:53:24Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [August 13, 2018, 5:53am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/21 "2018-08-13T05:53:39Z")

</div>

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._

---

<div class="post-metadata">

**Author:** ![\_tim-1](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/_tim-1/32/2639_2.png) [@\_tim-1](https://forum.wixstudio.com/u/_tim-1)\
**Post date:** [August 13, 2018, 4:21pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/22 "2018-08-13T16:21:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [August 14, 2018, 12:59pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/23 "2018-08-14T12:59:39Z")

</div>

Hey Tim, good point. Try this on page:

```javascript
$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.

---

<div class="post-metadata">

**Author:** ![\_tim-1](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/_tim-1/32/2639_2.png) [@\_tim-1](https://forum.wixstudio.com/u/_tim-1)\
**Post date:** [August 14, 2018, 2:17pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/24 "2018-08-14T14:17:35Z")

</div>

Thanks Yisrael. That did the trick.

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [August 15, 2018, 7:00am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/25 "2018-08-15T07:00:37Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![blairamirez8](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@blairamirez8](https://forum.wixstudio.com/u/blairamirez8)\
**Post date:** [August 15, 2018, 10:55am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/26 "2018-08-15T10:55:38Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [August 15, 2018, 11:44am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/27 "2018-08-15T11:44:51Z")

</div>

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](https://support.wix.com/en/wix-code/database-collections) and [Connecting Data](https://support.wix.com/en/wix-code/connecting-data) . Also, refer to the information about [Data hooks](https://support.wix.com/en/article/about-data-hooks) .

Good luck,

Yisrael

---

<div class="post-metadata">

**Author:** ![blairamirez8](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@blairamirez8](https://forum.wixstudio.com/u/blairamirez8)\
**Post date:** [August 15, 2018, 11:48am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/28 "2018-08-15T11:48:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [August 15, 2018, 11:50am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/29 "2018-08-15T11:50:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blairamirez8](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@blairamirez8](https://forum.wixstudio.com/u/blairamirez8)\
**Post date:** [August 15, 2018, 12:04pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/30 "2018-08-15T12:04:08Z")

</div>

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)

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

```

```javascript
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!

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [August 15, 2018, 12:06pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/31 "2018-08-15T12:06:11Z")

</div>

Hey Blai, maybe it’s magic. 🙃 Glad you got it working and for sharing your code. 🍻

---

<div class="post-metadata">

**Author:** ![ross95874](https://avatars.discourse-cdn.com/v4/letter/r/d2c977/32.png) [@ross95874](https://forum.wixstudio.com/u/ross95874)\
**Post date:** [October 6, 2018, 11:44pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/32 "2018-10-06T23:44:31Z")

</div>

# 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](https://www.wix.com/support/html5/)

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

Do you have an updated link ?

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [October 7, 2018, 5:24am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/33 "2018-10-07T05:24:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ross95874](https://avatars.discourse-cdn.com/v4/letter/r/d2c977/32.png) [@ross95874](https://forum.wixstudio.com/u/ross95874)\
**Post date:** [October 7, 2018, 5:55am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/34 "2018-10-07T05:55:55Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [October 7, 2018, 5:59am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/35 "2018-10-07T05:59:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ross95874](https://avatars.discourse-cdn.com/v4/letter/r/d2c977/32.png) [@ross95874](https://forum.wixstudio.com/u/ross95874)\
**Post date:** [October 7, 2018, 6:12am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/36 "2018-10-07T06:12:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [October 8, 2018, 7:03am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/37 "2018-10-08T07:03:21Z")

</div>

@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](https://www.wix.com/code/reference/wix-dataset.html#onAfterSave) for information on what triggers it and how its used.

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

---

<div class="post-metadata">

**Author:** ![hdspdc](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/hdspdc/32/6609_2.png) [@hdspdc](https://forum.wixstudio.com/u/hdspdc)\
**Post date:** [October 14, 2018, 3:08pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/38 "2018-10-14T15:08:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ralphedaniel](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@ralphedaniel](https://forum.wixstudio.com/u/ralphedaniel)\
**Post date:** [October 15, 2018, 4:51pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/39 "2018-10-15T16:51:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![code-queen](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-queen/32/696_2.png) [@code-queen](https://forum.wixstudio.com/u/code-queen)\
**Post date:** [October 15, 2018, 5:04pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/40 "2018-10-15T17:04:55Z")

</div>

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](https://www.youtube.com/watch?v=yLCOqsVHhD0)

[Previous page](https://forum.wixstudio.com/t/example-no-database-duplicates/33085.md?page=1)

[Next page](https://forum.wixstudio.com/t/example-no-database-duplicates/33085.md?page=3)
