Query on Coupons working perfectly in preview // not working live

Hi guys!

I built a code for auto generating a coupon code, everything works perfectly in preview, however I can’t see what is the problem on live. I don’t know why, but I always have problems on live VS preview :cry:

You can test the functionnality live : https://valentinloppe.wixsite.com/website-6/collection-1

Thank youuu !

The code :

import wixUsers from 'wix-users';
import { createCoupon } from 'backend/Coupons.jsw'
import wixData from 'wix-data';

$w.onReady(function () {
});

export async function button1_click(event) {
 let ID = await createtheCoupon(ID)
    console.log(ID)
 let identifiant = ID.id
    console.log(identifiant)
    identifiant = identifiant.toString()

    wixData.query("Marketing/Coupons")
        .eq("_id", identifiant)
        .find()
        .then((results) => {
            console.log(results)

            $w('#group1').expand()
 let firstItem = results.items[0]; //see item below
 let promo = firstItem.code
            promo = promo.toString()
            $w('#text13').text = promo

        })
        .catch((err) => {
 let errorMsg = err;
            console.log(errorMsg)

        });

}

function createtheCoupon(ID) {
 return createCoupon()
}

The code on backend/Coupons.jsw :

import { coupons } from 'wix-marketing-backend';

export function createCoupon() {
 let length = 5
 var result = '';
 var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 var charactersLength = characters.length;
 for (var i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    console.log(result)

 let couponData = {
 "name": result,
 "code": result,
 "startTime": new Date(),
 "limitPerCustomer": 1,
 "active": true,
 "scope": {
 "namespace": "stores"
        },
 "percentOffRate": 10
    };

 return coupons.createCoupon(couponData);
}

Make sure that you have the correct collection permissions and that you have synced the sandbox collection to Live .

Hi :slight_smile: Thanks for the answer !
Well, I can’t change the permissions on Coupons unfortunately, but I believe that running a query on this database isn’t suposed to be problematic, isn’t it ?
Thanks

Well, I agree that it’s most likely not a matter of collection permissions. However have you synced the sandbox collection to Live .

Also, you didn’t say what the problem is in Live. What isn’t working?

Well, since it’s a WIX database I don’t have the option to sync it :sweat_smile:

Regarding the problem on Live :
The backend function works fine, creates a new coupon and duly returns its ID. The problem seems to occur later, upon the query : I don’t get any error message in the console (despite the .catch()) on Chrome but the function seems to stop anyway and return nothing. So, everything that is supposed to happen after the query is just not happening…
I actually have the same issue with another website and the permissions on databases are all duly set up.
I will put another ice cream in the freezer for you :smiley:

Sometimes it takes time till my brain starts working…

OK, so now I understand. Your issue is that, as stated in the documentation , you can’t perform the query in the frontend since you need admin permissions:

When you run your site, since you are in the Editor, you are running as admin . However, when another user (not you) visits the site, that visitor doesn’t have the proper permissions. So, what you need to do is to perform the query in the backend and use the suppressAuth option.

Oh that’s pretty annoying… Is there any other way that I can give the newly created coupon to the user ? I mean, what’s the point of creating couons if we can’t display it ? :kissing:

Umm, first of all, you can perform a query in the backend, and return the result to the frontend.

However, what I don’t understand is what are you missing from the coupon create ? The call to createCoupon() returns the couponInfo object which should have the information you want.

Oh I am so stupid… you are right ! I just have to return the values instead of returning the ID and run a query in order to get these values !
Really, why I didn’t even think of that… Thank you so much !

" why I didn’t even think of that"

Well, 'cause you were looking for a reason to buy me a beer. And that’s not being stupid.

:rofl::rofl: Man I have a crush on Guiness too ! However, I might be a bit far for a drink (I a in Tahiti, pacific islands). Will surely drink one for you ! Thanks again

@loppe Ah - Guinness on the beach. Doesn’t get any better than that.

I am not a coder and this code is just what I’ve been looking for. How do I actually trigger this? The only way I can think of is to create a temporary page with a button. Clicking the button then adds a coupon to the database. This works, but I need to create 1000 coupons. Is this possible, and how would I adapt the code to do that?

And how do I get these codes to customers?