# Can we show beforeInsert hook error on frontEnd?

**URL:** <https://forum.wixstudio.com/t/can-we-show-beforeinsert-hook-error-on-frontend/11186>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [August 27, 2021, 1:29pm UTC](https://forum.wixstudio.com/t/can-we-show-beforeinsert-hook-error-on-frontend/11186 "2021-08-27T13:29:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wix-expert-velo-cert](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/wix-expert-velo-cert/32/2187_2.png) [@wix-expert-velo-cert](https://forum.wixstudio.com/u/wix-expert-velo-cert)\
**Post date:** [August 27, 2021, 1:29pm UTC](https://forum.wixstudio.com/t/can-we-show-beforeinsert-hook-error-on-frontend/11186/1 "2021-08-27T13:29:07Z")

</div>

Hi, I would like to stop duplicate entries using hook. Although, this code is working perfectly but I’m trying to take the result to front-end.

Is this possible to show error message on front-end?

If there is any improvement in code is possible then suggestions will be appreciated.

```javascript
//data.js
import wixData from 'wix-data';

export function duplicates (collection, field, item){
    return wixData.query(collection).eq(field, item[field])
    .find()
    .then((results)=>{
        return results.items.length
    })
    .catch((err) => {
 let errorMsg = err;
        console.log(errorMsg);
    }); 
}

export function email_beforeInsert(item, context) {
    console.log(item);
    console.log(context);
    return duplicates(context.collectionName, "title", item)
    .then((res)=>{
        if (res > 0){
            return Promise.reject(("this item exist"))
        }
        return item
    })
}

export function phonebook_onFailure(error, context) {
    let hookError = error;
    return hookError
}

```

---

<div class="post-metadata">

**Author:** ![bwprado](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/bwprado/32/7220_2.png) [@bwprado](https://forum.wixstudio.com/u/bwprado)\
**Post date:** [August 27, 2021, 2:21pm UTC](https://forum.wixstudio.com/t/can-we-show-beforeinsert-hook-error-on-frontend/11186/2 "2021-08-27T14:21:51Z")

</div>

You need to return the _ **err** _ being caught:

```javascript
.catch((errorMsg) => { 
    console.log(errorMsg)
    return errorMsg
})

```

---

<div class="post-metadata">

**Author:** ![bwprado](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/bwprado/32/7220_2.png) [@bwprado](https://forum.wixstudio.com/u/bwprado)\
**Post date:** [August 27, 2021, 2:24pm UTC](https://forum.wixstudio.com/t/can-we-show-beforeinsert-hook-error-on-frontend/11186/3 "2021-08-27T14:24:05Z")

</div>

Refactoring the first function:

```javascript
export function duplicates(collection, field, item) {
  return wixData
    .query(collection)
    .eq(field, item[field])
    .find()
    .then(results => results.items.length)
    .catch(errorMsg => return errorMsg)
}

```

---

<div class="post-metadata">

**Author:** ![wix-expert-velo-cert](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/wix-expert-velo-cert/32/2187_2.png) [@wix-expert-velo-cert](https://forum.wixstudio.com/u/wix-expert-velo-cert)\
**Post date:** [August 28, 2021, 6:36am UTC](https://forum.wixstudio.com/t/can-we-show-beforeinsert-hook-error-on-frontend/11186/4 "2021-08-28T06:36:09Z")

</div>

Hi, I tried this out. But I’m getting error only in console which I’m getting eariler too. I would like to show to the user a message that entered data already exist on front-end.

Is this possible to transfer data from data.js to page code?

Also, how to resolve onFailure hook? I’ve message like -  
" **Hook onFailure for collection result ignored! Expected hook result to resolve to an object with an ‘\_id’ property, but got [Undefined]**"
