While building a registration form on a website, I got stuck trying to check for a unique value of the user’s input
Here’s the relevant parts of my code:
Within the Page code:
import wixData from 'wix-data'
import (CheckUniqueValue) from 'backend/Data.jsw'
export function button9_click(event) {
var flag = false;
CheckUniqueValue("myCollection", "title", $w(“#input12").value).then( (result) => {
flag = result
})
console.log("Unique: " + flag)
}
backend/Data.jsw:
import wixData from 'wix-data';
export function CheckUniqueValue (collection, field, value) {
return wixData.query(collection)
.eq(field, value)
.find()
.then((results) => {
return !(results.items.length > 0)
})
}
No matter whether the value is unique or whether it already exists in the database, the flag always turns out false
What am I doing wrong?
Please note this part of the code is edited, omitting the unneeded parts
The process checks if the value is unique, proceeds to a purchase and only after payment result is successful - inserts to the database. This means I can’t have the value’s uniqueness checked within a beforeInsert hook, having to wait on a purchase as well.
Thanks in advance