Diesan Romero
Diesan Romero

Reputation: 1658

Property or method "name" is not defined on the instance but referenced during render in vuejs?

I am learning VueJS following a course in Udemy, and I have this code:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
        integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://kit.fontawesome.com/968b5b8bf4.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <div id="app">
        <div class="container">
            <div class="row">
                <div class="col-sm-12 col-md-6">
                    <div class="card">
                        <div class="card-header">Nuevo Usuario</div>
                        <div class="card-body">
                            <form>
                                <div class="form-group">
                                    <label for="name" >Nombre Completo</label>
                                    <input v-model="name" type="text" class="form-control" />
                                </div>
                                <div class="form-group">
                                    <label for="email">Correo Electrónico</label>
                                    <input v-model="email" type="email" class="form-control" />
                                </div>
                                <div class="form-group">
                                    <label for="password">Contraseña</label>
                                    <input v-model="password" type="password" class="form-control" />
                                </div>
                                <button @click.prevent="addUser" class="btn btn-primary btn-block">Ingresar Usuario</button>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12 col-md-6">
                    <div class="card">
                        <div class="card-header">Actualizar Usuario</div>
                        <div class="card-body">
                            <form>
                                <div class="form-group">
                                    <label for="name">Nombre Completo</label>
                                    <input type="text" class="form-control" />
                                </div>
                                <div class="form-group">
                                    <label for="email">Correo Electrónico</label>
                                    <inputt type="email" class="form-control" />
                                </div>
                                <div class="form-group">
                                    <label for="password">Contraseña</label>
                                    <input type="password" class="form-control" />
                                </div>
                                <button @click.prevent="modifyUser" class="btn btn-primary btn-block">Guardar Cambios</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12 mt-3">
                    <table class="table table-bordered table-striped">
                        <thead class="text-center">
                            <th>#</th>
                            <th>Nombre Completo</th>
                            <th>Correo Electrónico</th>
                            <th>Acciones</th>
                        </thead>
                        <tbody>
                            <tr class="text-center" v-for="(user, index) in users">
                                <td>{{ index + 1 }}</td>
                                <td>{{ user.name }}</td>
                                <td>{{ user.email }}</td>
                                <td>
                                    <button class="btn btn-success btn-sm">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                    <button class="btn btn-danger btn-sm">
                                        <i class="fas fa-trash-alt"></i>
                                    </button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <script src="main.js"></script>
</body>

</html>

and then, my VueJS code:

new Vue({
    el: '#app',
    data: {
        newUser: {
            name: '',
            email: '',
            password: '',
        },
        users: [],
    },
    methods: {
        addUser () {
            var nuevo = {}
            nuevo.name = this.newUser.name
            nuevo.email = this.newUser.email
            nuevo.password = this.newUser.password
            this.users.push(nuevo)
            this.newUser.name = ''
            this.newUser.email = ''
            this.newUser.password = ''
        },
    },
})

As you can see, I am creating a variable to save the data obtained by a form and send it to an array. Then use the v-for to iterate on each of them and have them displayed in a table. But I am getting this error:

vue.js:634 [Vue warn]: Property or method "name" 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.

Upvotes: 0

Views: 1996

Answers (1)

Hank X
Hank X

Reputation: 2044

 <input v-model="name" type="text" class="form-control" />

should be

 <input v-model="newUser.name" type="text" class="form-control" />

as you defined the data as {newUser: {name: ""}}.

do same to your other fields such as email

Upvotes: 1

Related Questions