# 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:** 1

<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:** [June 27, 2018, 11:53am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/1 "2018-06-27T11:53:24Z")

</div>

[#Example](https://www.wix.com/code/home/forum/search/posts%3Fquery=%23Example)[#Database](https://www.wix.com/code/home/forum/search/posts%3Fquery=%23Database)[#Hooks](https://www.wix.com/code/home/forum/search/posts%3Fquery=%23Hooks)[#beforeInsert](https://www.wix.com/code/home/forum/search/posts%3Fquery=%23beforeInsert)[#dataset](https://www.wix.com/code/home/forum/search/posts%3Fquery=%23dataset)

 ![](https://us1.discourse-cdn.com/wix/original/3X/c/6/c6ba844a0a05a23bf455b9e89573d1f46a3a1698.png)  
_The example doesn’t allow duplicates, hence only one beer._😞

**Demonstrates**

- [wix-data.Hooks](https://www.wix.com/code/reference/wix-data.Hooks.html)  
**Links for this example**

- [View the “Live” example in a browser page](https://www.wix.com/code-examples/no-db-duplicates-vw)

- [Open the Example template in the Wix Editor](https://editor.wix.com/html/editor/web/renderer/new?siteId=5f5b0cc1-f836-40f0-ae79-a843b405d43e&metaSiteId=d8355215-76e2-4782-b784-6091d561d7fc)  
**About this example**  
Using the [beforeInsert()](https://www.wix.com/code/reference/wix-data.Hooks.html#beforeInsert) data hook, you can check if the value to be inserted already exists in the database collection. If the value exists, the insert is rejected.

Note: you will find the code in the file **data.js** under the **Backend** folder:

![](https://us1.discourse-cdn.com/wix/original/3X/9/6/96a27c8c88c8f4eeb41edfd3f513cc37662fd79b.png)

---

<div class="post-metadata">

**Author:** ![matteoscarano2002](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/matteoscarano2002/32/5209_2.png) [@matteoscarano2002](https://forum.wixstudio.com/u/matteoscarano2002)\
**Post date:** [June 27, 2018, 1:37pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/2 "2018-06-27T13:37:43Z")

</div>

Would it be possible to have the code?

---

<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:** [June 27, 2018, 1:41pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/3 "2018-06-27T13:41:27Z")

</div>

Open the project in your editor from the link in the original post: [Open the Example template in the Wix Editor](https://editor.wix.com/html/editor/web/renderer/new?siteId=5f5b0cc1-f836-40f0-ae79-a843b405d43e&metaSiteId=d8355215-76e2-4782-b784-6091d561d7fc) . You will find all of the code there.

---

<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:** [June 27, 2018, 2:00pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/4 "2018-06-27T14:00:02Z")

</div>

Sorry, but I don’t understand. Please explain what isn’t working for you.

---

<div class="post-metadata">

**Author:** ![matteoscarano2002](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/matteoscarano2002/32/5209_2.png) [@matteoscarano2002](https://forum.wixstudio.com/u/matteoscarano2002)\
**Post date:** [June 27, 2018, 2:24pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/5 "2018-06-27T14:24:14Z")

</div>

I want to say that I found the code, but copying it to my other site I can not make it work.

---

<div class="post-metadata">

**Author:** ![matteoscarano2002](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/matteoscarano2002/32/5209_2.png) [@matteoscarano2002](https://forum.wixstudio.com/u/matteoscarano2002)\
**Post date:** [June 27, 2018, 3:14pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/6 "2018-06-27T15:14:03Z")

</div>

I’m using this code but it is not working.  
Could you help me?

Code:

**import** wixData **from** ‘wix-data’;

export function searchForDuplicates(value, info) {  
**return** wixData.query(info.MembersPublic)  
.eq(“userName”, value.names)  
.find()  
.then((results) =\> {  
**return** results.items.length;  
})  
. **catch** ((err) =\> {  
**let** errorMsg = err;  
})  
}

export function MembersPublic\_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.  
**return** searchForDuplicates(item, context).then((res) =\> {  
**if** (res \> 0) {  
**return** Promise.reject(‘This item already exists’);  
}  
**return** item;  
});  
}

$w.onReady( **function** () {  
$w(“#submitDataset”).onAfterSave(() =\> {  
$w(‘#displayDataset’).refresh(); // refresh list after new item saved  
});  
});

---

<div class="post-metadata">

**Author:** ![matteoscarano2002](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/matteoscarano2002/32/5209_2.png) [@matteoscarano2002](https://forum.wixstudio.com/u/matteoscarano2002)\
**Post date:** [June 27, 2018, 5:02pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/7 "2018-06-27T17:02:47Z")

</div>

?

---

<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:** [June 28, 2018, 6:16am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/8 "2018-06-28T06:16:39Z")

</div>

@Matteo - if your collection **Field Key** is **userName** , then you need to use that for **value** as well:

```javascript
.eq("userName", value.userName) 

```

You need to adapt the code for your own use. Collection fields, component IDs, etc need to be changed to what your site is using.

---

<div class="post-metadata">

**Author:** ![matteoscarano2002](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/matteoscarano2002/32/5209_2.png) [@matteoscarano2002](https://forum.wixstudio.com/u/matteoscarano2002)\
**Post date:** [June 28, 2018, 6:51am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/9 "2018-06-28T06:51:43Z")

</div>

Thanks, I managed to resolve the problem.

---

<div class="post-metadata">

**Author:** ![maxwelllassissi](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/maxwelllassissi/32/5497_2.png) [@maxwelllassissi](https://forum.wixstudio.com/u/maxwelllassissi)\
**Post date:** [July 18, 2018, 10:55pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/10 "2018-07-18T22:55:24Z")

</div>

Hello Yisrael and Matteo

I followed your example code in order to prevent duplicate in my collection field (Title). But it doesn’t work. I don’t know what wrong with tha code even if i follow step by step coding. Please have a look on it. My Field Key is “title”

**import** wixData **from** ‘wix-data’;

export function searchForDuplicates(value, info) {  
**return** wixData.query(info.Annonceurs)  
.eq(“title”, value.title)  
.find()  
.then((results) =\> {  
**return** results.items.length;  
})  
. **catch** ((err) =\> {  
**let** errorMsg = err;  
})  
}

export function Annonceurs\_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.  
**return** searchForDuplicates(item, context).then((res) =\> {  
**if** (res \> 0) {  
**return** Promise.reject(‘This item already exists’);  
}  
**return** item;  
});  
}

For the second part of coding i delete repeater line cause i don’t need it at all.

**import** wixData **from** ‘wix-data’;

$w.onReady( **function** () {  
$w(“#datasetA”).onAfterSave(() =\> {  
});  
});

![](https://us1.discourse-cdn.com/wix/original/3X/e/1/e175a2d3b42e7890752454b230048c312e7323c2.png)  
In summerise collectionName = Annonceurs, or not ? In your case, collectionName is different to noDuplicationDB.

Sincerely,

---

<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:** [July 19, 2018, 5:29am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/11 "2018-07-19T05:29:08Z")

</div>

The **beforeInsert** function uses the name of the collection as a way to create the hook. The **searchForDuplicates** function can be used for hooks from different collections by using the **collectionName** property of the **context** object which is passed in the **info** parameter.

In the example, **wixData.query** uses **info.collectionName** which is equal to “noDuplicatesDB”.

Instead of this:  
**return wixData.query(info.Annonceurs)**

You should do this:  
**return wixData.query(info.collectionName)**  
or  
**return wixData.query(“Annonceurs”)**

Using **info.collectionName** allows you to use the **searchForDuplicates** function for multiple collections.

I hope this helps,

Yisrael

---

<div class="post-metadata">

**Author:** ![maxwelllassissi](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/maxwelllassissi/32/5497_2.png) [@maxwelllassissi](https://forum.wixstudio.com/u/maxwelllassissi)\
**Post date:** [July 19, 2018, 3:45pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/12 "2018-07-19T15:45:03Z")

</div>

Thanks Yisrael, the code works as well. But, the message “this value already exists” doesn’t appear when the user request failed.

How may i fixe it?

---

<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:** [July 22, 2018, 7:42am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/13 "2018-07-22T07:42:59Z")

</div>

Maxwell,

I’m sorry, but I don’t understand your problem. If you are having a problem, you need to carefully check the example code.

A full tested and working example template is available to load into your editor. You can modify the code for your own use.

Yisrael

---

<div class="post-metadata">

**Author:** ![Shan](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/shan/32/106059_2.png) [@Shan](https://forum.wixstudio.com/u/Shan)\
**Post date:** [July 28, 2018, 7:19am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/14 "2018-07-28T07:19:10Z")

</div>

Works like a charm, Thanks 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:** [July 31, 2018, 4:12pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/15 "2018-07-31T16:12:04Z")

</div>

Hey Yisrael!

Thank you very much, it’s working really well to me but I wonder if I could use it for two different fields.

My code right now is:

```javascript
import wixData from 'wix-data';
import wixUsers from 'wix-users';

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("email", value.email)
        .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;
    });
}

```

Should I do a searchForDuplicates2 or what could I do? I need that their Company Names are not duplicated neither…

Thank you very much!

---

<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 1, 2018, 10:27am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/16 "2018-08-01T10:27:54Z")

</div>

You can either put two queries in the the searchForDuplicates() function, or create a searchForDuplicates2() function for the other check. Call them both from the beforeUpdate() hook. Make sure you handle the promises correctly.

---

<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 10, 2018, 5:11pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/17 "2018-08-10T17:11:24Z")

</div>

Hi Yisrael,

I’m no able to make it work.

I have 2 questions:

1. Do you see something wrong? I’m doing everything that you say and it’s not working. I think I don’t get what you say about promises. I connect the buttons with the Submit and the Delete options, not coding.

```javascript
import wixLocation from 'wix-location';
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 Company_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;
    });
}

export function Company_afterRemove(item, context) {
    wixLocation.to("www.weaallgopr.com")
}

```

I tried return wixData.query(“Company”) and  
return wixData.query(info.collectionName).

The second problem is…

1. I want a hook to redirect to the homepage (imagine the web is [weaallgopr.com](http://weaallgopr.com)) after deleting the profile.

Thank you very much ☹

---

<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 10, 2018, 5:12pm UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/18 "2018-08-10T17:12:19Z")

</div>

And I forgot to say that I’m working with a Read & Edit database, that’s why I use a lot of times beforeUpdate. But with beforeInsert is not working neither! \>.\<

---

<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:** [August 11, 2018, 4:39am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/19 "2018-08-11T04:39:36Z")

</div>

Thank you for this code!!! I was able to create a tutorial video using an example of this code!!!

I am not sure if it will help anyone that is having trouble. I used it to check for duplicates when a member creates/updates a username in their profile page.

I will leave it here just in case!

Link: [Create Member Website with Different Types of Members - Build Profile Page - YouTube](https://www.youtube.com/watch?v=yLCOqsVHhD0&feature=youtu.be)

---

<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 12, 2018, 8:57am UTC](https://forum.wixstudio.com/t/example-no-database-duplicates/33085/20 "2018-08-12T08:57:34Z")

</div>

hahaha i follow u on youtube. It’s still not working for me… I have exactly the same but instead of beforeInsert, beforeUpdate

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