[SOLVED] Hook not working on live data

Hi! I am trying to build a simple website, containing a form. The form has the following fields:

  • idTranzactie
  • idCasa
  • data
  • idBon
    I want to concatenate all these fields in order to create a separate field in my collection, called “title”. “title” has to be unique for each item - I have to stop inserting duplicates. In order to do this, I wrote the following code in my data.js file:
import wixData from 'wix-data';

export function searchForDuplicates(collection, field, item) {
 return wixData.query(collection)
        .eq(field, item.idTranzactie + item.idCasa + item.data + item.idBon)
        .find()
        .then((results) => {
               return results.items.length;
        })
        .catch((err) => {
               let errorMsg = err;
        });
}

export function Participanti_beforeInsert(item, context) {   
 return searchForDuplicates(context.collectionName, "title", item).then((res) => {
        if(res > 0) {   
               return Promise.reject('Duplicate!');
        }
        item.title = item.idTranzactie + item.idCasa + item.data + item.idBon;
        return Promise.resolve(item);
    });
}

The code works perfectly in preview mode, but on the live site it looks like the hook is not even called and there can be inserted duplicates.

Can someone help me debug this issue, please? I need my live site to be working perfectly in the next 3 days!

Hello. It’s pretty hard to say what’s wrong. Ussually if it’s working in preview and doesn’t work in live site related to collection permissions. I suggest to check them first. Also I see that you catch your error in searchForDuplicates , but you don’t check it. Since it happens on backend, I also suggest to use Site monitoring .

Thank you for your help! Using Site monitoring I realized I had a WD_PERMISSION_DENIED error. After changing permissions on the database it worked perfectly on the live site.