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?
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.
Please post the URL of your site. Only authorized Wix personnel can get access to your site in the editor.