1

I am getting this error but in search console I am only getting [Vue warn]: Error in render: "TypeError: Cannot read property 'title' of undefined"

When I go to check what line the error is coming from,it only highlights the first line where the variable is being initiated "var shoppingCartApp = new Vue({"

Is there a way of debugging this to show me which line 'title' is on?

Is there another reason I am getting this error?

<style>
    * {
        margin: 0;
        padding: 0;
    }
    #shoppingCartApp {
        width: 100%;
        max-width: 800px;
        margin: auto;
    }
    button:hover {
        cursor: pointer;
        background: #B7053F !important;
    }
</style>

<div id="shoppingCartApp">

    <!-- First Page -->
    <div id="scPage-1">
        <div class="text-center">
            <h1>{{ mainTitle }}</h1>
        </div>
        <!-- Promo Code Section -->
        <div class="scPromoCode text-center">
            <input type="text" v-model="userPackageContent.promoCodeValue" /><button v-on:click="showPromoValue(userPackageContent.promoCodeValue)">Apply</button>
        </div>
        <!-- End Promo Code Section -->
        <br />
        <!-- Country Selection Section -->
        <div class="scCountries row text-center">
            <button class="usaButton col-sm" v-on:click="showUsaPrice()">USA</button>
            <button class="canButton col-sm" v-on:click="showCanPrice()">Canada</button>
            <select class="intButton col-sm" id="scCountryDropdown" name="country" v-on:click="showIntPrice()">
                <option value="International">International</option>
                <option value="Afganistan">Afghanistan</option>
                <option value="Albania">Albania</option>
                <option value="Algeria">Algeria</option>
            </select>
        </div>
        <!-- End Country Selection Section -->
        <br />
        <!-- Subscription Selection Section -->
        <div class="scJournalSelections row text-center">
            <div class="dailySubscription col-sm">
                <h2>{{ subscriptionInfo.dailySubcription.title }}</h2>
                <h2>${{ subscriptionInfo.dailySubcription.price }}</h2>
                <button v-on:click="getCurrentSubscriptionChoice($event)" value="Daily">Subscribe</button>
            </div>
            <div class="journalSubscription col-sm">
                <h2>{{ subscriptionInfo.journalSubcription.title }}</h2>
                <h2>${{ subscriptionInfo.journalSubcription.price }}</h2>
                <button v-on:click="getCurrentSubscriptionChoice($event)" value="Journal">Subscribe</button>
            </div>
            <div class="dualSubscription col-sm">
                <h2>{{ subscriptionInfo.dualSubcription.title }}</h2>
                <h2>${{ subscriptionInfo.dualSubcription.price }}</h2>
                <button v-on:click="getCurrentSubscriptionChoice($event)" value="Daily & Journal">Subscribe</button>
            </div>
        </div>
        <!-- End Subscription Selection Section -->
    </div>
    <!-- End First Page -->

    <div id="scPage-2" class="text-center">
        <h1>Login/Registration Page If Not Already Logged In</h1>
    </div>


    <div id="scPage-3" class="text-center">
        <h1>Delivery Address</h1>
    </div>

    <div id="scPage-4" class="text-center">
        <h1>Cart Details</h1>
        <div class="scPromoCode text-center">
            <input type="text" v-model="userPackageContent.promoCodeValue" /><button v-on:click="showPromoValue(userPackageContent.promoCodeValue)">Apply</button>
        </div>
        <p>Subscription: {{ userPackageContent.subscriptionChoiceTitle }}</p>
        <p>Promo Discount: {{ userPackageContent.promoDiscount }}</p>
    </div>

    <div id="scPage-5" class="text-center">
        <h1>Checkout</h1>
        <p>Total: ${{ userPackageContent.checkoutTotal }}</p>
        <p>Total: ${{ userPackageContent.salesTax }}</p>
    </div>

</div>







    <script>
        var shoppingCartApp = new Vue({
            el: '#shoppingCartApp',
            data: {
                mainTitle: 'Shopping Cart',
                //Promo Discount Information
                promoValidation: true,
                usaProps: {
                    promoCode: 'usa',
                    promoDiscount: 50,
                    subscriptionPrice: 2,
                    value: false
                },
                canProps: {
                    promoCode: 'can',
                    promoDiscount: 75,
                    subscriptionPrice: 4,
                    value: false
                },
                intProps: {
                    promoCode: 'int',
                    promoDiscount: 100,
                    subscriptionPrice: 6,
                    value: false
                },
                dualProps: {
                    promoCode: 'dual',
                    promoDiscount: 150,
                    subscriptionPrice: 8,
                    value: false
                },
                //Package Information
                subscriptionInfo: {
                    dailySubscription: {
                        price: 100,
                        title: 'Daily'
                    },
                    journalSubscription: {
                        price: 200,
                        title: 'Journal'
                    },
                    dualSubscription: {
                        price: 300,
                        title: 'Daily & Journal'
                    }
                },
                //User Checkout Information
                userPackageContent: {
                    subscriptionChoiceTitle: null,
                    subscriptionChoicePrice: null,
                    promoCodeValue: null,
                    promoDiscount: 0,
                    salesTax: 0,
                    checkoutTotal: null
                }

            },
            methods: {
                getCurrentSubscriptionChoice: function (e) {
                    var subscriptionValue = e.target.value;


                    if (subscriptionValue == this.subscriptionInfo.dailySubcription.title) {
                        this.userPackageContent.subscriptionChoiceTitle = this.subscriptionInfo.dailySubcription.title;
                        this.userPackageContent.subscriptionChoicePrice = this.subscriptionInfo.dailySubcription.price;
                        this.userPackageContent.checkoutTotal = this.userPackageContent.subscriptionChoicePrice;
                    }
                    if (subscriptionValue == this.subscriptionInfo.journalSubcription.title) {
                        this.userPackageContent.subscriptionChoiceTitle = this.subscriptionInfo.journalSubcription.title;
                        this.userPackageContent.subscriptionChoicePrice = this.subscriptionInfo.journalSubcription.price;
                        this.userPackageContent.checkoutTotal = this.userPackageContent.subscriptionChoicePrice;
                    }
                    if (subscriptionValue == this.subscriptionInfo.dualSubcription.title) {
                        this.userPackageContent.subscriptionChoiceTitle = this.subscriptionInfo.dualSubcription.title;
                        this.userPackageContent.subscriptionChoicePrice = this.subscriptionInfo.dualSubcription.price;
                        this.userPackageContent.checkoutTotal = this.userPackageContent.subscriptionChoicePrice;
                    }
                    console.log(this.userPackageContent.subscriptionChoiceTitle)
                    console.log(this.userPackageContent.subscriptionChoicePrice)

                },
                showCurrentCountrySelection: function () {
                    if (this.usaProps.value == true) {
                        $('.usaButton').css('background', 'rgba(171, 183, 183, 1)')
                    } else {
                        $('.usaButton').css('background', '#fff')
                    }
                    if (this.canProps.value == true) {
                        $('.canButton').css('background', 'rgba(171, 183, 183, 1)')
                    } else {
                        $('.canButton').css('background', '#fff')
                    }
                    if (this.intProps.value == true) {
                        $('.intButton').css('background', 'rgba(171, 183, 183, 1)')
                    } else {
                        $('.intButton').css('background', '#fff')
                    }
                },
                subscriptionChangePriceReset: function () {
                    this.subscriptionInfo.dailySubscription.price = 100;
                    this.subscriptionInfo.journalSubscription.price = 200;
                    this.subscriptionInfo.dualSubscription.price = 300;

                    this.promoValidation = true;
                },
                showPromoValue: function (value) {
                    if (value == "usa") {
                        if (this.usaProps.value == true && promoValidation) {
                            this.subscriptionInfo.dailySubscription.price = this.subscriptionInfo.dailySubscription.price - this.usaProps.promoDiscount;
                            this.subscriptionInfo.journalSubscription.price = this.subscriptionInfo.journalSubscription.price - this.usaProps.promoDiscount;
                            this.subscriptionInfo.dualSubscription.price = this.subscriptionInfo.dualSubscription.price - this.usaProps.promoDiscount;

                            this.promoValidation = false;
                        }
                    }
                    if (value == "can") {
                        if (this.canProps.value == true && promoValidation) {
                            this.subscriptionInfo.dailySubscription.price = this.subscriptionInfo.dailySubscription.price - this.canProps.promoDiscount;
                            this.subscriptionInfo.journalSubscription.price = this.subscriptionInfo.journalSubscription.price - this.canProps.promoDiscount;
                            this.subscriptionInfo.dualSubscription.price = this.subscriptionInfo.dualSubscription.price - this.canProps.promoDiscount;

                            this.promoValidation = false;
                        }
                    }
                    if (value == "int") {
                        if (this.int == true && promoValidation) {
                            this.subscriptionInfo.dailySubscription.price = this.subscriptionInfo.dailySubscription.price - this.intProps.promoDiscount;
                            this.subscriptionInfo.journalSubscription.price = this.subscriptionInfo.journalSubscription.price - this.intProps.promoDiscount;
                            this.subscriptionInfo.dualSubscription.price = this.subscriptionInfo.dualSubscription.price - this.intProps.promoDiscount;

                            this.promoValidation = false;
                        }
                    }
                },
                showUsaPrice: function () {
                    this.subscriptionChangePriceReset();
                    //Used to prevent promo code from constantly being applied
                    this.usaProps.value = true;
                    this.canProps.value = false;
                    this.intProps.value = false;

                    this.showCurrentCountrySelection();

                    this.subscriptionInfo.dailySubscription.price = this.subscriptionInfo.dailySubscription.price * this.usaProps.price; //API PRICE GOES HERE
                    this.subscriptionInfo.journalSubscription.price = this.subscriptionInfo.journalSubscription.price * this.usaProps.price; //API PRICE GOES HERE
                    this.subscriptionInfo.dualSubscription.price = this.subscriptionInfo.dualSubscription.price * this.usaProps.price; //API PRICE GOES HERE
                },
                showCanPrice: function () {
                    this.subscriptionChangePriceReset();
                    //Used to prevent promo code from constantly being applied
                    this.usaProps.value = false;
                    this.canProps.value = true;
                    this.intProps.value = false;

                    this.showCurrentCountrySelection();

                    this.subscriptionInfo.dailySubscription.price = this.subscriptionInfo.dailySubscription.price * this.canProps.price; //API PRICE GOES HERE
                    this.subscriptionInfo.journalSubscription.price = this.subscriptionInfo.journalSubscription.price * this.canProps.price; //API PRICE GOES HERE
                    this.subscriptionInfo.dualSubscription.price = this.subscriptionInfo.dualSubscription.price * this.canProps.price; //API PRICE GOES HERE
                },
                showIntPrice: function () {
                    this.subscriptionChangePriceReset();
                    //Used to prevent promo code from constantly being applied
                    this.usaProps.value = false;
                    this.canProps.value = false;
                    this.intProps.value = true;

                    this.showCurrentCountrySelection();

                    this.subscriptionInfo.dailySubscription.price = this.subscriptionInfo.dailySubscription.price * this.intProps.price; //API PRICE GOES HERE
                    this.subscriptionInfo.journalSubscription.price = this.subscriptionInfo.journalSubscription.price * this.intProps.price; //API PRICE GOES HERE
                    this.subscriptionInfo.dualSubscription.price = this.subscriptionInfo.dualSubscription.price * this.intProps.price; //API PRICE GOES HERE
                }
            }
        })

        console.log("Script running")
    </script>

1 Answer 1

1

You need to check data before getting a deep value of title.

So try something like this:

if (subscriptionValue == this.subscriptionInfo &&
 this.subscriptionInfo.dailySubcription &&
 this.subscriptionInfo.dailySubcription.title)

Also, in your component do:

{{ subscriptionInfo.dailySubcription && subscriptionInfo.dailySubcription.title }}

Just Change in all places where you have deep values and this should work.

Some helpful links: link-1, link-2.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for this and the references. I changed this for both the title and the price and the error has been resolved but the values are still not showing for me. I will read through the links you have given me and see what I can do from here!
Ok, did you check carefully for typos?
after tons of reading and trying.....yes...it was a typo lol thanks!
You are welcome if the answer solved your error, can you please mark it as answer. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.