Avoid Duplicates in dataset using Hook & beforeInsert Function

I’m trying to use a dataset hook on our Irish Whiskey Club inputs in order to avoid adding duplicates from a members inputs (that is, if they enter the same Whiskey more than once it doesn’t get saved in the dataset).

The dataset (Clubinput) has the Whiskey name in column 1, the date in column 2 and text comments in column 3. The dataset also has the inputs from all the members. The code always returns “Duplicate” even tho there are no duplicates (yet). I don’t see why the code is doing that. Any suggestions?

Also, I think I need to query the dataset for both the Whiskey and the currentuserID in the code before checking for a duplicate? I don’t know how to do that either. Any more suggestions?

Here’s my data.js code.

import wixData from 'wix-data'; 
export function searchForDuplicates(value) { 
return wixData.query("Clubinput") 
.eq("Whiskey", value) 
.find() 
.then((results) => {
 return results.items.length;
 }) 
 .catch((err) => { let errorMsg = err; 
 });
 } 
 export function Clubinput_beforeInsert(item) { 
 return searchForDuplicates(item.Whiskey) .then((res) => { 
 if(res > 0) { 
 return Promise.reject("Duplicate");
  } return item;
  });
  }

I would recommend posting this in the Corvid forum if you don’t get a response in the next day or so. :slight_smile:

Thanks, will do!