Can we show beforeInsert hook error on frontEnd?

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.

//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
}

You need to return the err being caught:

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

Refactoring the first function:

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


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] "