Solved

Shopify Functions Discount Example - Cannot read properties of undefined (reading 'session')

Megaohms
Shopify Partner
3 0 0

Hello I have been following the tutorial here https://shopify.dev/docs/apps/selling-strategies/discounts/experience/ui

 

Everything has been working up until the frontend part, I have been checking the code back and forth but can not see what I have done different to the example code. If anyone can help with the following errors I would be very grateful! 

 

When I click Save in the discount form I get the following errors in the terminal.

 

.../web/index.js:82
 web-backend │ session: res.locals.shopify.session
 web-backend │ ^
web-backend │ TypeError: Cannot read properties of undefined (reading 'session')
at runDiscountMutation (.../web/index.js:82:33)
web-backend │.../web/index.js:58:9

 

And these errors in the console.

 

XHR POST https://.../api/discounts/automatic

[HTTP/2 500 Internal Server Error 101ms]

 

Uncaught (in promise) SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

 

Here is the json request from the POST

 

{
"discount": {
"functionId": "*******************************",
"combinesWith": {
"orderDiscounts": false,
"productDiscounts": false,
"shippingDiscounts": false
},
"startsAt": "2023-07-07T09:32:21.415Z",
"endsAt": null,
"metafields": [{
"namespace": "$app:order-discount",
"key": "function-configuration",
"type": "json",
"value": "{\"percentage\":20,\"amount\":20,\"maximum\":100}"
}],
"title": "testing"
}
}

 

 

 

Accepted Solution (1)

lizk
Shopify Staff
246 58 71

This is an accepted solution.

Without seeing your full repository it may be hard to debug where something went wrong.
The error message TypeError: Cannot read properties of undefined (reading 'session')  means the Shopify object is undefined. You'll want to make sure you added the runDiscountMutation after the app.use(express.json()), as stated in the instructions.

If you want to review the finished code sample for the discount tutorial you can review the instructions here to clone the project. (There is a small unrelated bug with this repo if you try to run it. Workaround documented here currently.)

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 2 (2)

lizk
Shopify Staff
246 58 71

This is an accepted solution.

Without seeing your full repository it may be hard to debug where something went wrong.
The error message TypeError: Cannot read properties of undefined (reading 'session')  means the Shopify object is undefined. You'll want to make sure you added the runDiscountMutation after the app.use(express.json()), as stated in the instructions.

If you want to review the finished code sample for the discount tutorial you can review the instructions here to clone the project. (There is a small unrelated bug with this repo if you try to run it. Workaround documented here currently.)

To learn more visit the Shopify Help Center or the Community Blog.

Megaohms
Shopify Partner
3 0 0

Hi thanks for your help. I could not see anything I had missed... created the app from scratch and all is working so I must have made some tiny mistake somewhere!

 

Now I this working is there any details on how to let the user edit the discount code or is it just a case of piecing it together from the example app on github?