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!