Check and delete duplicate items on database

Hi again! I coded the function to check and delete duplicate items which has the same “giaCode” when I import the csv to my wix database. But it doesn’t work. Those duplicate items are inserted when I import the csv files.

My data.js is below:

import wixData from ‘wix-data’;

let duplicateData;

export function searchForDuplicates(value, info) {
// info contains the hook context
// use the collectionName property to use function for multiple collections
return wixData.query(“Diamond”)
.eq(“giaCode”)
.find()
.then((results) => {
return results.items.length;
})
. catch ((err) => {
let errorMsg = err;
})
}

async function removeItems(items) {
items.forEach( async (item, i) => {
await wixData.remove(“Diamond”, items[i]._id);
console.log(removeItems);
});
}

export function Diamond_beforeInsert(item, context) {
return searchForDuplicates(item, context).then((res) => {
if (res > 0) {
removeItems(res.items);
}
return item;
});
}

Thanks for your help =]

Hi; I am trying to solve the same problem. If you found the solution, can you share the sample code?