searching for duplicates before inputting into database

Simple form to input info into my database… that all works… I want to add a function that checks the input on 1 field (title(key) or coa(name) field), then accepting or rejecting the input from the form. have found tutorials to follow to enter into the data.js file, and nothing is working… granted could be me i am new to js. and i have lots of questions on this code also.

Live page

import wixData from ‘wix-data’ ;

//question do the parameters stay these vague names or do i change them to the collection and field name in my db?? i have tried both and neither work
export function searchforduplicates ( collection , field , item ) {

return wixData . query ( “coa” )
. eq ( “title” , “coa” ) //title is the field key, coa is the field name
. find ()
. then (( results ) => {
if ( results.items.length ){
console . log ( results.items [ 0 ]); //see firstItem below
} else {
// handle case where no matching items found
}

}) 
. catch (( err ) => { 
    console . log ( err ); 
    **return**  Promise . reject ( 'An error occured on wix query' )  
//this is the only error that comes up in the debug 
}) 

}

//same question about the parameter names
export function COA_beforeInsert ( item , context ) {
let hookContext = context ;
//TODO: write your code here…
return searchforduplicates ( context.collection , “coa” , item ). then (( res ) => {
if ( res > 0 ){
return Promise . reject ( ‘COA Number Already Exists!’ )
}
return item
})
}

the debugger gives me these errors and of course i have no idea why it always tells me twice:
[beforeSave event] triggered on wix-dataset
UserError: datasetApi ‘save’ operation failed
Caused by ServerValidationError: Record saving failed
Caused by Error: An error occured on wix query
Caused by: ServerValidationError: Record saving failed
Caused by Error: An error occured on wix query
[datasetError event] triggered on wix-dataset with (
Array(2) json Table Copy JSON
0: “save”
1: “An error occured on wix query” )

Greetings Sarah

Some things I think are wrong with your code:

  1. remove the ‘return’ from the wixdata.Query(“coa”) also you need to include the name of your collection here (is ‘coa’ the name of your collection or the name of title value? You are querying your collection/dataset here.

  2. Your if statement is not evaluating anything. I think it should be if(results.items.length == 0) {then do something like console.log(results.item[0]} else {do something else}

On my sign up webpage of my site, I check to make sure the email addressed inputted on on text box input is not a duplicate first, my code below may help you

//Check Duplicate Email; if duplicate exists show warning and kept password box disabled
export function checkEmailDup ( event )
{
let email = $w ( ‘#registerEmail’ ). value . toLowerCase ();
console . log ( “User added email in the EMAIL input box” )

//Query for Existing Email in Collection; if found, do not allow user to register & generate 'duplication error' 
wixData . query ( "MemberInfo" ) 
. eq ( "email" ,  email ) 
. find () 
. then ( ( results ) =>  
{ 
    //If the email field already exists; show error 
    **if** ( results.length  >  0 ) 
    { 
        $w ( "#emailDuperror" ). expand (); 
        $w ( '#registerPassword' ). disable (); 
        console . log ( "Email Already Exists! Password field disabled!" ) 
    } 
    **else** 
    { 
        $w ( '#registerPassword' ). enable (); 
        $w ( '#emailDuperror' ). collapse (); 
    } 
}) 

}

On my actual input box for users to enter the email I check to make sure its not a duplicate

//On Email Input, check if email is already in database (must be unique) 
$w ( '#registerEmail' ). onBlur (( Event ) => 
{ 
    checkEmailDup ( Event ); 
}) 

Basically, I check for a duplicate BEFORE I enable my submit button to allow a user to submit data to my database/collection. It ensures that there are no duplicates. If the email address is already in the database, I disable the password and ‘submit’ button fields and the user can not enter the data into the collection.

Hope this helps, if not. I can help out more.

Good luck

aLvin

hi there!
COA is the name of my db
field I need to check is the field name: coa, field key: title
couple questions. I got it to check and grey out the submit button when the COA number field is dupped, but the error message doesn’t pop up.

import wixData from ‘wix-data’ ;
$w . onReady ( function () {
// Write your code here
});

//Check Duplicate coa number; if duplicate exists show warning and kept password box disabled
export function checkCOAduplicate ( event )
{
let coaNumber = $w ( ‘#inputCOA’ ). value . toLowerCase ();
console . log ( “User added coa number in the coa input box” )

//Query for Existing Email in Collection; if found, do not allow user to register & generate 'duplication error' 
wixData . query ( "COA" ) 
. eq ( "title" ,  coaNumber ) 
. find () 
. then ( ( results ) =>  
{ 
    //If the coa field already exists; show error 
    **if** ( results.length  >  0 ) 
    { 
    $w ( "#errorMessage" ). expand (); 
    $w ( '#submitButton' ). disable (); 
    console . log ( "COA Number Already Exists!" ) 
    } 
    **else** 
    { 
     $w ( '#errorMessage' ). collapse (); 
    } 
}) 

}

$w ( ‘#inputCOA’ ). onBlur (( Event ) =>
{
checkCOAduplicate ( Event );
})

Hi Mrs. Sarah

  1. Yes, I put all the code on my page file (i.e. everything is all coded on my ‘sign up’ page

  1. #registerEmail is not a field key in my database. #registerEmail is just the name of my textelement for my inputbox. That’s where a user would enter there email address

  1. Yes, "MemberInfo’ is the name of my database. (database = collection)

Yeah I’ve been coding on Wix for just over a year and I was sooo lost in the beginning. Don’t worry, it gets easier over time. YouTube, this wix forum, & the wix API (https://www.wix.com/velo/reference/$w) was a lifesaver. Hoping the pictures help

hi there!
COA is the name of my db
field I need to check is the field name: coa, field key: title
couple questions. I got it to check and grey out the submit button when the COA number field is dupped, but the error message doesn’t pop up.

import wixData from ‘wix-data’ ;

$w . onReady ( function () {
// Write your code here
});

//Check Duplicate coa number; if duplicate exists show warning and kept password box disabled
export function checkCOAduplicate ( event ) {
let coaNumber = $w ( ‘#inputCOA’ ). value . toLowerCase ();
console . log ( “User added coa number in the coa input box” )

//Query for Existing Email in Collection; if found, do not allow user to register & generate ‘duplication error’
wixData . query ( “COA” )
. eq ( “title” , coaNumber )
. find () . then ( ( results ) => {

//If the coa field already exists; show error
if ( results.length > 0 ){
$w ( “#errorMessage” ). expand (); //does not expand
$w ( ‘#submitButton’ ). disable (); //submit button does grey out
console . log ( “COA Number Already Exists!” ) //this message does appear in console log
}
else {
$w ( ‘#errorMessage’ ). collapse ();
}
})
}
$w ( ‘#inputCOA’ ). onBlur (( Event ) =>
{
checkCOAduplicate ( Event );
})

This issue has been resolved?
If so, mark it as —> RESOLVED ← —> giving your HELPER a → BEST-ANSWER , or at least a → LIKE !
If not, let your helper know about that.

Hello Mrs. Sarah

Great job on making some progress in the right direction. Every move in the right direction is a win.

For the error message:
I created a text element and called it ‘errorMessage’ and I set it to collapse.
i.e → $w(’ #errorMessage ').collapse() .

This basically ‘hide’ the text element by default. Whenever the other input box detect a duplication it simply displays that text element saying “Duplication Error: Email Already Exists, try again”.

So all you need to do is create your own errorMessage text element. You can set it to .hide() or .collapse() and when a dup is detected just have that message .show() or .expand()

aLvin

ok… the errorMessage and submitButton are both connected to the elements but the submitButton disables but only while in the “coaInput” box, once the user tabs out to fill out the form it enables. I did get the error message to show (i had it set to hidden instead of collapse facepalm).

import wixData from ‘wix-data’;

//Check Duplicate coa number; if duplicate exists show warning and kept password box disabled
export function checkCOAduplicate (Event)
{
let coaNumber = $w(‘#inputCOA’).value.toLowerCase();
console.log(“User added coa number in the coa input box”)

//Query for Existing Email in Collection; if found, do not allow user to register & generate 'duplication error' 
wixData.query("COA") 
.eq("title", coaNumber) 
.find() 
.then( (results) =>  
{ 
    //If the coa field already exists; show error 
    **if** (results.length > 0) 
    { 
    $w('#errorMessage').expand();   
    $w('#submitButton').disable();  
    console.log("COA Number Already Exists!") 
    } 
    **else** 
    { 
     $w('#errorMessage').collapse(); 
     $w('#submitButton').enable(); 
    } 
}) 

}

$w(‘#inputCOA’).onBlur((Event) =>
{
checkCOAduplicate(Event);
})