- Creatively Nino
- May 10, 2020
- 1 min read
Updated: Nov 1, 2020
Here is the helpful code to get you going! Let me know if you need any more assistance in the comments on YouTube or contact me at the bottom today!
What Was Covered: - Add User Input Fields
- Add a Button
- Create a Wix Data Collection
- Connect Database to User Input Elements
- Add a hook to your database
- Write simple lines of code to not insert duplicate values
- Use Corvid to complete the action
- Test the Before Insert Hook in the Preview Window
Code Snippet to follow along with the video:
Backend Code
import wixData from 'wix-data';
export function searchforduplicates(collection, field, item) {
let options = {
"suppressAuth": true,
"suppressHooks": true
};
return wixData.query(collection).eq(field, item[field]).find(options).then((results) => {
return results.items.length
}).catch((err) => {
let errorMsg = err;
console.log(err);
return Promise.reject('An error occured on wix query')
})
}
export function event_beforeInsert(item, context) {
//TODO: write your code here...
return searchforduplicates(context.collectionName, "email", item).then((res) => {
if (res > 0) {
return Promise.reject('This item exist!')
}
return item
})
}
Comments