beforesave issue

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;
});
}

Maybe you need to add return before wixData.query(“moadon”) so it becomes return wixData.query(“moadon”). What do you get in the results.items.length if you just run that separate and console log that value?

Hi,
I think Andreas is right.
If it’s not working please provide a link to your site so we can inspect.
Roi.