Cannot read propert 'coupons' of undefined

So, i know this type of error got questioned alot, but im unable to find the right solution for my problem.
Im totally new with corvid, with some progamming knowledge in other languages, but just superficial.

So what i am trying to achieve is to sell coupons for some services in the bookings app.

How my plan is: I have 1 product (a Coupon) in the Wix Shop app with different options, which will result in different prices (for different services in the bookings app later on). Someone buys his item in the store ( the coupon) and then my code should generate the coupon in wix and send it to him per mail.

So, thats the goal, that he can later give this code to someone as a present and this person can use it for the service.

Im far away from that, i just started looking into it but i ran into this error
“TypeError: Cannot read property ‘coupons’ of undefined”

My code is very short, i just copied the example to create a coupon from the api, it looks like this:

import wixMarketing from 'wix-marketing-backend';

export function createCoupon() {
	//Add your code for this event here: 
	let couponData = {
		"name": "My Coupon",
		"code": "myCouponCode",
		"startTime": new Date(),
		"scope": {
			"namespace": "bookings" // Coupon applies to all booking services
		},
		"moneyOffAmount": 49
	};

	return wixMarketing.coupons.createCoupon(couponData);
}

$w.onReady(function () {
	//TODO: write your page related code here...
createCoupon();
});

I wrote this code into a test page, just to test if it generates the coupon “My Coupon” for 49€ if i load the page (i tested it with a button before), and i cant see where the error is, maybe someone of you could give me a tip?
This could wont do what i want it to do in the end, but i wanted the “generate a coupon” part to work before i try to send it per mail to the customer.

Thanks in advance!

Hauke

As you can see from the import statement, wix-marketing-backend is a backend API and can only be used in backend code. You will need to move that code into a web module (.jsw) file, and call it from your page code. For information on how to do that, see the article Calling Server-Side Code from the Front-End .

Thanks for your answer, i changed it and now i dont get any errors anymore.
But its still not working and i cant find the error. As a beginner in this topic, its fairly hard to understand those API documentations.

My backend code:

import wixMarketing from 'wix-marketing-backend';

export function createCoupon() {
    let couponData = {
        name: "name",
        code: 'myCouponCode',
        startTime: new Date(),
        expirationTime: new Date(2020, 12, 31),
        active:    true,
        scope: {
        namespace: 'stores',
    },
    moneyOffAmount: 10,
};
return wixMarketing.createCoupon(couponData);
}

My whole frontend code for the testpage:

import {createCoupon} from 'backend/couponGenerator.jsw';

$w.onReady(function () {
    createCoupon().then(id => {
    console.log(id);
})
.catch(err => {
    console.log(err);
});
});

If i open this page i dont get any (error) messages in the developer console except { … }, but if i look into the coupons database there is no new coupon ( there are no coupons at all)

I checked in the forum and someone got it working with exactly this code, are there any requirements i am missing? something that isn’t listed on the API page?

push, bc thread got lost on page 3 :slight_smile:

Anyone able to help? Still working on it :slight_smile: