Hi, I have a before insert hook to check for duplicate entries but when debugging I am getting a “Did you forget to resolve a promise?” error and timeout. I have copied the code below, thanks.
"import wixData from ‘wix-data’;
export function Members_beforeInsert(item, context) {
console.log(“Inside function?”);
let p = new Promise(function (resolve, reject) {
console.log(“Inside promise”);
let email_q = wixData.query(“c1”).eq(“name”, item.name);
email_q.count().then(count => {
console.log(“after it returns”);
if (count === 0) {
console.log(“count is zero, no dups found, resolving the promise to the original ‘item’ so the insert can proceed”);
resolve(item);
} else {
console.log(“count is more than zero, rejecting the promise with an error message”);
reject(“count is more than 0, dups found!”);
}
});
});
return p;
}"
Hello Terrence,
Apologies for the late reply.
We are checking your question and reply as soon as we have feedback.
Thank you. Please do not forget
Hey Terrence,
Just thought I’d butt in here and let you know I tried your code (only changing the collection name and the field name) and it worked perfectly. I have to say pretty cool.
So, it looks like some investigation is needed…
-
Are you trying this on the sandboxed or live site?
-
Are you sure the name of the collection (c1) is correct?
-
Are you sure the name of the field (name) is correct?
-
Is there data in the collection? (does it even matter?)
-
The double quotes (") at the beginning and end of the code is just on the forum post or do you have that in your site code? (I deleted the quotes for my tests - the code doesn’t work with them.)
OK, be kind - just remember that “there are no stupid questions”
Get back to me when you can and I’ll dig further.
Yisrael
I am having the exact some problem with the exact same code. Here’s my code:
import wixData from ‘wix-data’;
export function users_beforeInsert(item, context) {
let p = new Promise(function(resolve, reject)
{
//create a query to look for items with the same name as the item we're tring to insert
let username_q = wixData.query("users").eq("username", item.username);
//create a query to look for items with the same email as the item we're tring to insert
let email_q = wixData.query("users").eq("email", item.email);
//create a query to look for items with the same name OR the same email as the item we're trying to insert
let big_q = username_q.or(email_q);
//run the query to count the number of items that match
big_q.count().then(count =>
{
if (count === 0)
{
resolve(item);
}
else
{
reject("count is more than 0, dups found!");
}
});
});
//return the promise from the hook
return p;
}
Here is the issue I’m having. I created a form to connct to my database of USERS. After playing with it, I found that someone could create duplicate accounts. So, I looked around to find some tips on preventing the same user from creating the same account over and over. I then found that code listed above. I tweaked it to fit the information I needed for what I was trying to do. NOW, in the Sandbox mode, it worked great. I could enter new users, and if I tried to create the same account twice, BAM…it was refused, so all in all, everything worked great. I saved my work, published it, then went to the site to try it out in LIVE mode. That’s when I realized it wouldn’t go any further. I entered the information into the form, clicked the SIGN UP button, and nothing…just sat there. So I went to check my database, and nope…didn’t insert the information i entered. So that is my problem.