How to query database to see if Unique Code entered in member registration form matches available & assigned codes and show error if not?

Each of our new members is required to have a Unique code that they enter in the sign up sheet. How do I search the code against the database to see if it is available?

Defined Terms:

Form Name: Registrationform5
Text Field I want to validate " input8 "
Database/collection I want to search " AllAvailableCodes "
FieldName I want to search “Input 8” values against: " AllCodes " (column 1)
FieldName I want to validate: " Assignedcodes " (column 2)
Submit Button: " Button2 "

After the user clicks the submit (" Button2 "), the query should search the value inputted in to input8 against the Field " AllCodes " in the database collection of available codes: " AllAvailableCodes "

If input8 does NOT match any value in field " AllCodes " then:
—> it should result in error message that says “Code Invalid” and member registration form should not submit.

If the input8 value matches any value in field " AllCodes " then:
→ it should validate there has been a “1” inputted in to column 2, field name " AssignedCodes ". If there is a “1”, then submit successfully.
→ If there is no “1” it should send an error message “Code Not Assigned”

Here is my Code so far but I don’t know how to finish it:


import wixData from 'wix-data'

export function button2_click(event,$w) {
wixData.query("AllAvailableCodes") 
.eq("AllCodes", $w('#input8').value) 
.eq("Assignedcodes", 0)
.find() 
.then(res => { 
if (res.length = 0) 
{ //show error message - existing cancelation form } 
 
} 
}) 
} 

Thank you!!

Hello Thrower Away,

This could help you a little bit…

while reading your post and looking at your code, i saw some strange facts.

FieldName I want to search “Input 8” values against: " AllCodes " (column 1)
Do not forget!!! Column1 is always —> “title”
“AllCodes” is column-2.

Then i saw you write the references with UperCase like this —>" AllCodes ".
Sure that your reference-ID (column-ID) is written with upper-case-alphabetics? (check this out by go into the database and looking for column-ID). " Assignedcodes" the same!

wixData.query("AllAvailableCodes") 
  .find()
  .then( (results) => {
 if(results.items.length > 0) {
 let itemsAmount = results.length
 let firstItem = results.items[0]; 
 
 //looking if the entered value is in ---> AllCodes [if ---> YES then ----> ??? do something   /   if ---> NO ---> do something else]
 for (var i = 0; i < itemsAmount; i++) {
 if ($w('#input8').value == results.items[i].AllCodes){  }   // <------- Matching items found here (YES)
 else {   }  // <------- No matching items found here (NO)
    }

    } else {    }
  } )
  .catch( (err) => {
 let errorMsg = err;
  } );

The same way like shown here in this example (with one for-loop), you can also check Assignedcodes for “0/1”-value.

Good luck.

Thank you for responding! Unfortunately this code string is not working as I am still receiving membership signup forms from people who have entered codes that are not on the data collections. I am unsure how to proceed from here.