1
votes

I am creating shopping shopping cart in vue js . I am want to display username into label when user provide correct informations . I am getting following errors in google chrome windows .

vue.js:634 [Vue warn]: Property or method "users" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties. (found in )

Here is my html code .

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="bootstap.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js">

    </script>
</head>
<body>
    <div class="container" id="app">
        <nav class="navbar navbar-expand-sm bg-dark navbar-dark">
            <ul class="navbar-nav">


                <li class="nav-item">
                    <a class="nav-link" href="index.html">Show All Products</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/signin.html">Signin</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="cart.html"> cart</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/index.html" v-on:click="logout">Logout</a>
                </li>


            </ul>
            <div class="nav-item active" v-for="user in users" >
                <label>Username:{{username}}</label>
            </div>

        </nav>
        <br />



        <select class="form-control" id="sel1" v-on:change="applyfilters($event.target.value)">
            <option value="">Select Any value</option>

            <option v-for="v in vendors" :value="v.id">{{ v.name }} </option>
            <!-- <option value="v.id">{{v.name}}</option>-->
            <!--<option value="2">MI</option>-->
        </select>
        <br />
        <div class="row col-12" id="product-list">
            <div class="col-4 card mx-2 p-2" v-for="product in products" style="margin-bottom: 20px">
                <p>Product id:<b />{{product.id}}</p>
                <b>Product Name :</b>{{product.name}}
                <div class="row">
                    <div class="col-4 m-3 p-3">
                        <b>Price :</b>  {{product.price}}
                    </div>
                    <div class="col-4 m-3 p-3">
                        <b>Vendor :</b>  {{product.vendor.name}}
                    </div>
                    <div class="col-6 m-2 p-3">
                        <button class="col btn btn-primary" v-on:click="addToCart(product.id)">Buy</button>
                    </div>
                </div>
            </div>
            <br />
        </div>
    </div>
</body>
</html>
<script>
    let app = new Vue({
        el: "#app",
        data: {
            newTask: '',
            id: 0,
            url: '/todos',
            status: false,
            products: [],
            vendors: []
        },
        methods: {
            getAllProducts() {
                new Promise((resolve) => {
                    axios.get('/api/products').then(function (response) {
                        resolve(response.data)
                    })
                }).then((data) => {
                    this.products = data
                    // console.log(this.products)
                })
            },
           getusers() {
                new Promise((resolve) => {
                    axios.get('/api/users').then(function (response) {
                        resolve(response.data)
                    })
                }).then((data) => {
                    this.users = data

                })
            },




            addToCart(id) {
                console.log(id)
                var obj = { productId: id };
                console.log(obj)
                new Promise((resolve) => {
                    axios.post('/api/cart/', obj).then(function (response) {
                        resolve(response.data)
                    })
                }).then((data) => {

                    console.log(data)
                    console.log(data.id)
                    if (!data.id) {

                        // console.log("fist login")
                        // window.alert("Fist login ")
                        //  window.location = "signin.html";
                    }
                    else {
                        // console.log("successfully add to cart")
                        window.alert("product has been added to your cart")
                    }

                })
            },
            findAllVendors() {
                new Promise((resolve) => {
                    axios.get('/api/vendor').then(function (data) {
                        resolve(data.data)
                        // console.log(data.data)
                    })
                }).then((data) => {
                    this.vendors = data
                })
            },
            applyfilters(id) {
                new Promise((resolve) => {
                    axios.get('/api/products/' + id).then(function (response) {
                        resolve(response.data)
                    })
                }).then((data) => {
                    this.products = data
                    // console.log(this.products)
                })
            }
        }
    })
    app.getAllProducts();
    app.findAllVendors();</script>

Hers is screen shot output . enter image description here

1
The error message includes instructions on what you need to do to fix the problem. Could you explain what further information you require?skirtle

1 Answers

3
votes
data: {
  users: [],
  newTask: '',
  id: 0,
  url: '/todos',
  status: false,
  products: [],
  vendors: []
},

Your data must look like this. It's because you're calling this.users or users in template but it is not defined on the instance. You need to define properties in data to make it reactive too.