skip to Main Content

main code where error is occuring

In checkout.js file, when I’m creating a deliveryOption variable to store different deliveryOptions(which is in data/deliveryOptions.js file) and this logic is working completely fine in live server but when i pushed it to github, the checkout page is not displaying any items with this error in the console:

console of github pages showing the error

Here you can checkout the project in my github(https://github.com/Sushant-Luitel/Amazon-project)

i looked for this error in chatgpt and it indicated that the error is in the deliveryOption variable which is presumed to be undefined. However , i consoled deliveryOption.deliveryDays in my local server and it was displaying correct value of 7 .

Next i checked for the type coercion and it was correct too.
i tried to check by changing the variable name as well and it was still the same.
Everything is working perfectly fine as it should in my local server but not in github pages.(and yes I didn’t forget to push in github) .
After 10 failed attempts i am looking for help here .

Also if you go to the trouble of looking at my github. The only related files to look at are deliveryOptions.js, cart.js and checkout.js

2

Answers


  1. Chosen as BEST ANSWER

    Update: The problem is fixed. LocalStorage was the culprit. It messed up everything. I just cleared the localstorage and it worked


  2. Here your deliveryOption variable is undefied;
    Here if the if condition isn’t true deliveryOption variable stay undefined. Of course if the variable is undefined this code will throw an error since you can’t retrieve a value by property from undefined.

    To avoid an error you can use ?. sign.

    deliveryOption?.deliveryDays
    

    And this expression will returns an undefined without an error throwing.

    Also you can prepare your value by default.

    deliveryOption?.deliveryDays || "default"
    

    Then if the expression returns undefined you get "default" string except of undefined.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search