Search for duplicate entires?

Hi There,

I now have over 100 sign ups on my website. I want to know if there is any easy way to check for duplicate entries.

Basically, I want to easily identify if someone has registered twice.

Any filter that checks for duplicate entires?
Whether it be a name, number, or email?

Hi Anthony,
finding duplicates is doable, but not easy, using wix-data.
I will give an outline for how to do it below, but before that, I think there’s a different and simpler approach.

instead of finding dups after the fact, why not prevent them from occurring in the first place?
you can do that relatively easily using data hooks .

all you need to do is implement a “before insert” hook (and maybe a “before update” hook if you let users update their profile), that looks for profiles with the same properties, and reject the insert/update operation if you find anything.

to look for dups, you would create a wix-data query using the OR operator.
the query will look for items that have the relevant email OR the relevant number OR the relevant name.
if it finds anything, your code will reject the operation and return some error code so you can notify your user to change their data.

====

now for finding dups after the fact.
what you can do it run a query that retrieves all records sorted by email (for example).
then go over them one by one, and perform a check to see if the email is the same as the email for the previous item.
if you get two emails that are the same, it means you have a dup.

you can do that for every field (number, name in your example).

the question is, once you find a dup, what then?
for this reason, preventing dups is a much better solution (and better user experience for your users).

hope this helps!

1 Like

That’s Ziv!
What exactly do I write in the line of code?

Is it really possible to signup twice using the standard membership dialogs?

Ziv,

I’m having issues with the hook. What exactly do I insert in that line of code.

Hi Anthony,
the general idea is this.

  • create a query to find items in the collection that have same email
  • create a query to find items in the collection that have same number
  • create a query to find items in the collection that have same name
  • use the query or() operator to combine the queries into a single one.
  • now run the combined query’s count() operation to count the number of duplicate items in the collection
  • in the then() part of the count operation, do a check:
    – if the count is zero, it means there are no dups. return the original item to resolve the promise.
    – else, it means there are dups. reject the promise to abort the insert operation.

I hope this will guide you through it, if you find it too high level let me know.

thanks and good luck!

1 Like

Ziv,

This isnt working at all.
I just need to block individuals who already signed up.

Hi Anthony.

check out this code sample.
I hope it does what you meant for it to do.

import wixData from 'wix-data'

/* INSERT HOOK SAMPLE CODE - ABORT THE INSERT IF DUPS FOUND
 *
 * assumptions:
 * 1. collection name is c1
 * CHANGE THE FUNCTION NAME FROM c1_beforeInsert TO yourCollectionName_beforeInsert
 * 
 * 2. collection has these fields: name(text), email(text), number(number)
 * CHANGE THESE VALUES TO VALUES THAT MATCH YOUR RELEVANT COLLECTION FIELD NAMES!
 * 
 * the code looks for items in the collection that have the same name or email or number.
 * if it finds any - the insert is aborted
 * 
 */
export function c1_beforeInsert(item, context) {
	
	//all log messages will show when we run this code.
	//they will help us debug and see all is working well.
	//they have no impact other than that and can be safely removed
	//once we see the code is working well
	console.log("in before insert hook");
	console.log("name is " + item.name);
	console.log("email is " + item.email);
	console.log("number is " + item.number);
	
	//create a new promise object. 
	//in the promise's body we'll look for dups.
	//if we find any, we will reject the promise and abort the insert.
	let p = new Promise(function(resolve, reject)
	{
		console.log("in promise body");
		
		//create a query to look for items with the same name as the item we're tring to insert
		let name_q = wixData.query("c1").eq("name", item.name);

		//create a query to look for items with the same email as the item we're tring to insert
		let email_q = wixData.query("c1").eq("email", item.email);

		//create a query to look for items with the same number as the item we're tring to insert
		let number_q = wixData.query("c1").eq("number", item.number);

		//create a query to look for items with the same name OR the same number OR the same email as the item we're tring to insert
		let big_q = name_q.or(email_q).or(number_q);
		
		//run the query to count the number of items that match
		big_q.count().then(count =>
		{
			console.log("after big_q returns");
			if (count === 0)
			{
				console.log("count is zero, no dups found, resolving the promise to the original 'item' so the insert can proceed");			
				resolve(item);
			}
			else
			{
				console.log("count is more than zero, rejecting the promise with an error message");
				reject("count is more than 0, dups found!");
			}
		});
	});
	
	//return the promise from the hook
	return p;
}

Let me know if this makes any sense…

thanks!

2 Likes

Ziv,

i’m half way there… still a bit confused.
I replaced the info based on my functions and still nothing.

This is all I need to use to prevent dups, right?


Also, what message will the user see when they are rejected?

Hi Anthony,
so a few things.

first, I forgot to mention that you have to change your function name from c1_beforeInsert to be
yourCollectionName _beforeInsert.

you need to replace yourCollectionName with the name of the collection data is inserted to.

secondly, please paste the entire code from this file so I can take a look and guide you further.
thanks!

p.s.
edited the code snippet above to reflect the need to change the function name.

Based on the file you provided - i don’t know what to exclude or keep, since i don’t code.

This isn’t making sense to me

Hi again Anthony,
I will contact you via email and we’ll decide how to proceed.
thanks.

1 Like

Thanks

note to self: duplicate entries, duplicate entry, duplicate submission

I also copied the above code in my hooks and edited the code as my collection and field names but i got an error as below.
"save operation failed: Error: WebMethod request timed-out after 14 seconds… Did you forget to resolve a promise? "
Thanks.

When I try the code above, the collection is not allowing new data to be inserted. I tried using different as well as duplicate email addresses and none work. Also, where do I display the error message if the user to registering again (duplicate email address)? How do I notify that their email address exists and that they have already registered?