import wixData from ‘wix-data’;
export function searchForDuplicates(value, info) {
// info contains the hook context
// use the collectionName property to use function for multiple collections
return wixData.query(“Submit”)
.eq(“phone”, value.phone) //why is this highlighted and underlined?
.eq(“username”, value.username)
.find()
.then((results) => {
return results.items.length;
})
. catch ((err) => {
let errorMsg = err;
});
}
export function Submit_beforeInsert(item, context) {
// Calls routine to check if value already exists in the collection.
// If value not found, then save record.
// Else, if value is found, then reject this insert to prevent duplicate values.
// Note: concurrent inserts may result in duplicates.
// Pass context to searchForDuplicates to use for multiple collections.
return searchForDuplicates(item, context).then((res) => {
if (res > 0) {
return Promise.reject(‘This item already exists’);
}
return item;
});
}
//Im just trying to search the Submit database for duplicate phones and duplicate usernames. It works with just one but i can’t seem to search for both, I have two user inputs and a submit button, I essentially want users to add their username in input 1, phone in input 2, click submit and have the backend do a data query to find if either the phone or username is already in the database.
Hi, .
Does replacing the following line:
.eq("username", value.username)
with the following lines:
.or(
wixData.query("Submit")
.eq("username", value.username)
)
make it work for you?
For the phone underlined query, simply check that it is phone and not something else like phones or mainPhone, otherwise ignore it.
Make sure that you have setup your data hooks in the data.js file in your backend correctly.
https://www.wix.com/corvid/reference/wix-data.Hooks.html
https://support.wix.com/en/article/corvid-using-data-hooks
https://www.wix.com/corvid/reference/wix-data.Hooks.html#beforeInsert
https://support.wix.com/en/article/corvid-tutorial-processing-user-input-before-it-is-stored-in-a-collection-with-data-hooks
https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#eq
https://www.wix.com/corvid/reference/wix-data.html#query
Have a look at this example from Nayeli which shows the full page code and backend code for checking for duplicates.
https://codequeen.wixsite.com/membership-dashboard/the-code
https://codequeen.wixsite.com/membership-dashboard/the-backend-code
https://www.youtube.com/watch?v=yLCOqsVHhD0&feature=youtu.be - This video does work, so if it happens to say error on loading when you open it, simply leave it for a minute and it should reload itself and play the video.
Or do something like Abergquist suggested with your code, whatever works for you.
wixData.query("Submit")
.eq("username", value.username)
.or(wixData.query("Submit")
.eq("phone", [value.phone](http://value.phone/) )
.find()
.then((results) => {
Perfect thank you so much