How do I query an object within a database field

I’m using wixCode to create a custom booking page, and I would like to incorporate coupon code validation. I’m running into trouble when I try to validate the selected service by using the entityID.

import wixData from ‘wix-data’;

export function codeValidationButton_click(event) {
let couponCode = $w(‘code’).value;
let service = selectedService // value is the ‘entityID’, located in the scope field of coupon database

wixData.query('Marketing/Coupons') 
    .eq('active',  **true** ) 
    .eq('code', couponCode) 
    .contains('scope', service) //  *scope field contains an object, can I query the object?* 
    .find() 
    .then((results) => { 
        console.log(results); 
    }) 

}

I know I could validate the ID the after the query returns the results (maybe this is the best solution) but I would love an elegant way to include the validation in the query.

Thanks in advance!

instead of .contains use . hasSome

…returns a similar error message:

.hasSome():
Error: {“message”:“Unknown token scope → Map($hasSome → List(246d8072-1b95-49db-b60b-2a5a2a5afdab))”,“details”:{}}

.contains():
Error: {“message”:“Unknown token scope → Map($contains → 246d8072-1b95-49db-b60b-2a5a2a5afdab)”,“details”:{}}

Hi Scott, did you ever find a solution for this? I am facing the same issue right now. Appreciate if you can share what you have discovered, so far.
Thanks

It’s been a while, but I believe the issue was with the permission setting of Marketing > Coupons. Wix does not allow the “read content” setting to be changed from Admin to Site Content .

The MacGyvered solution arrived at was to create a new database (titled “coupon_validation” in the example below) which has the following fields:

couponCode | serviceId | startDate | endDate

Under General Permissions (“What is this collection for?”) select Site Content.

couponCode should be the same as the coupon code you set up in Marketing > Coupons
serviceId should match the service ID for the service in Bookings > Services that you want to apply the discount.

Then call the new database to validate the serviceId and couponCode. If found, add the coupon code to the “options” variable for checkout.

//set when user clicks on service
let selectedService = serviceIdNum;

// include this as the options in Wixbooking checkout
let options = {
  "paymentType": "wixPay_Offline",
  "couponCode": ''
 };
function couponValidation() {
  let input = $w('#coupon');
  let code = input.value.toLowerCase();  
  
  if (code.length > 0) {
    wixData.query('coupon_validation')
      .eq('couponCode', code)
      .eq('serviceId', selectedService)
      .find()
      .then((results) => {
          console.log(results)
          if (results.items.length > 0) { 
             // You could add startDate / endDate 
             // validation here as well.
            options.couponCode = code;
            } else {
            options.couponCode = '';
            }
        });
     }
 }

Let me know if you find a better solution. I would love to run all of the validation out of the Marketing > Coupons database and eliminate the need to maintain a second database.

Has anyone found a solution for this. I’m having same issue trying to query the Stores\Products table which has an additional info field that is an object field. I want to be able to query that field, but I get the same error message from above.