For some reason when I try to query a dataset being used by a Lightbox subscribers form, it always returns empty even though there are hundreds of items in it. I am querying using this example: https://support.wix.com/en/article/corvid-working-with-the-data-api#querying-your-collection-1
What am I doing wrong?
Lightbox code:
import {sendEmail} from ‘backend/contact’ ;
$w.onReady( function () {
});
export function button2_click(event) {
sendEmail($w( ’ #input1 ’ ).value);
}
Backend code:
export function sendEmail(email) {
var exists = false;
wixData.query("lightboxSubscribe03")
.eq("emails.email", email) //returns empty set even if this line is commented out
.find()
.then( (results) => {
console.log(results.items); //always returns empty set
if(results.totalCount > 0) {
exists = true;
}
} );
if (email.includes("@") && email.includes(".") && !exists) {
wixCRM.createContact({
"firstName": email,
"emails": [email]
})
.then((contactId) => {
var coupons = createCoup(email);
return wixCRM.emailContact("new_sub", contactId, {
"variables": {
"couponCode": coupons[3],
"couponCode2": coupons[4],
"couponCode3": coupons[5]
}
})
.then(() => {
})
.catch((err) => {
console.log(err);
});
});
}
}
Do you have a database collection called lightboxSubscribe03 ?
Thanks for getting back to me. Yes I believe I do, unless a Content Collection is different than a Database Collection. Here’s a screenshot:
Please post the URL of your site. Only authorized Wix personnel can get access to your site in the editor.
OK, so it seems like this…
First of all, if you are trying to query the collection in Preview, you have no data in the sandbox so the query will come up empty. But you probably already know that.
The permissions on the collection allow form submission, but only allow admin to read from the collection. You need to use the suppressAuth option in the backend query to read the collection. Add to the find() like this:
.find({"suppressAuth": true})
Your filter is also incorrect. It should be:
.eq("email", email)
Using the corrections I detailed above, I tried this myself and it worked.
Thank you! The one issue I am having now is that it seems to only be working when I am in preview, and then when I try on the published site I am still generating the coupon codes.
@akbutler22 Well, if I understand correctly what you are trying to do, I’d say it’s because you are checking if the user gets a coupon in the wrong place.
The issue you are having is a slight lack of understanding of how Promises work - which is what the .then() function on the query is. The query runs asynchronously - that is, you can’t really tell when it will finish. All you know is that the .then() function runs when the query is completed. You are setting the variable exists inside the .then() which means that you are setting exists to true after you are checking it outside the .then() .
You can read about Promises here:
The first article explains about queries and Promises and will help you get things straightened out.
@yisrael-wix I see, thank you. I think the other issue is that the collection’s read permissions were set to Admin online, instead of Anyone. Is this dangerous to have a collection that stores emails available for read access to Anyone?
@akbutler22 You don’t want to set to Anyone. This is the reason for the suppressAuth option which allows the backend code to read regardless of who the current user is. Backend code is by definition the Admin. So, backend code is allowed to run a query using suppressAuth.
@akbutler22 Also, the issue with the Promises will cause the logic to evaluate exists at the wrong time.
@yisrael-wix Got it working! Thank you so much for all of your help, I’ve been stuck on this for weeks.