Open the Example template in the Wix Editor About this example
Using the beforeInsert() data hook, you can check if the value to be inserted already exists in the database collection. If the value exists, the insert is rejected.
Note: you will find the code in the file data.js under the Backend folder:
export function MembersPublic_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. return searchForDuplicates(item, context).then((res) => { if (res > 0) { return Promise.reject(‘This item already exists’);
} return item;
});
}
$w.onReady( function () {
$w(“#submitDataset”).onAfterSave(() => {
$w(‘#displayDataset’).refresh(); // refresh list after new item saved
});
});
I followed your example code in order to prevent duplicate in my collection field (Title). But it doesn’t work. I don’t know what wrong with tha code even if i follow step by step coding. Please have a look on it. My Field Key is “title”
export function Annonceurs_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. return searchForDuplicates(item, context).then((res) => { if (res > 0) { return Promise.reject(‘This item already exists’);
} return item;
});
}
For the second part of coding i delete repeater line cause i don’t need it at all.
import wixData from ‘wix-data’;
$w.onReady( function () {
$w(“#datasetA”).onAfterSave(() => {
});
});
In summerise collectionName = Annonceurs, or not ? In your case, collectionName is different to noDuplicationDB.
The beforeInsert function uses the name of the collection as a way to create the hook. The searchForDuplicates function can be used for hooks from different collections by using the collectionName property of the context object which is passed in the info parameter.
In the example, wixData.query uses info.collectionName which is equal to “noDuplicatesDB”.
Instead of this: return wixData.query(info.Annonceurs)
You should do this: return wixData.query(info.collectionName)
or return wixData.query(“Annonceurs”)
Using info.collectionName allows you to use the searchForDuplicates function for multiple collections.
Thank you very much, it’s working really well to me but I wonder if I could use it for two different fields.
My code right now is:
import wixData from 'wix-data';
import wixUsers from 'wix-users';
export function searchForDuplicates(value, info) {
// info contains the hook context
// use the collectionName property to use function for multiple collections
return wixData.query("Company")
.eq("email", value.email)
.find()
.then((results) => {
return results.items.length;
})
.catch((err) => {
let errorMsg = err;
})
}
export function Company_beforeUpdate(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;
});
}
Should I do a searchForDuplicates2 or what could I do? I need that their Company Names are not duplicated neither…
You can either put two queries in the the searchForDuplicates() function, or create a searchForDuplicates2() function for the other check. Call them both from the beforeUpdate() hook. Make sure you handle the promises correctly.
Do you see something wrong? I’m doing everything that you say and it’s not working. I think I don’t get what you say about promises. I connect the buttons with the Submit and the Delete options, not coding.
import wixLocation from 'wix-location';
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("Company")
.eq("companyName", value.companyName)
.find()
.then((results) => {
return results.items.length;
})
.catch((err) => {
let errorMsg = err;
})
}
export function Company_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;
});
}
export function Company_afterRemove(item, context) {
wixLocation.to("www.weaallgopr.com")
}
I tried return wixData.query(“Company”) and
return wixData.query(info.collectionName).
The second problem is…
I want a hook to redirect to the homepage (imagine the web is weaallgopr.com) after deleting the profile.
And I forgot to say that I’m working with a Read & Edit database, that’s why I use a lot of times beforeUpdate. But with beforeInsert is not working neither! >.<
Thank you for this code!!! I was able to create a tutorial video using an example of this code!!!
I am not sure if it will help anyone that is having trouble. I used it to check for duplicates when a member creates/updates a username in their profile page.