whats wrong in this code?
export function moadon_beforeInsert(item, context) {
console.log(“js beforeinsert”);
return searchForDuplicates(context.collectionName, “cell”, item).then((res) => {
if (res > 0) {
console.log(“inside if”);
return Promise.reject(‘EXIST’);
}
console.log(“else”);
wixData.query(“moadon”)
.limit(1)
.descending(‘_createdDate’) // descending() sorts the results in descending order by property, i choose “_createdDate”
.find()
.then((results) => {
const lastItemInCollection = results.items[0];
item.validation = item.validation;
item.branch=item.branch;
console.log(“item is:” + item);
return item;
});
});
}
export function searchForDuplicates(collection, field, item) {
// info contains the hook context
// use the collectionName property to use function for multiple collections
return wixData.query(collection)
.eq(field, item[field])
.find()
.then((results) => {
return results.items.length;
})
. catch ((err) => {
let errorMsg = err;
});
}