1

I got the following error:

TypeError: Cannot read property 'name' of undefined

Vue file:

<tr v-for="user in fields.data" :key="user.id">
    <td>{{ user.id }}</td>
    <td>{{ user.name }}</td>
    <td>{{ user.email }}</td>
    <td>{{ user.password }}</td>
    <td>
        <a href="#" data-toggle="modal" data-target="#modal" @click="edit(user.id)">
            <span class="btn btn-primary">
                <i class="mdi mdi-edit"></i>
            </span>
        </a>
    </td>
</tr>

Script:

edit(id) {
    this.users = this.fields[id];
    console.log(this.users);
},
8
  • what do you have in fields.data ? Commented Jul 23, 2019 at 18:17
  • its nothing, sorry, i accidentally added it. Commented Jul 23, 2019 at 18:19
  • Possible duplicate of Angular2: Cannot read property 'name' of undefined Commented Jul 23, 2019 at 18:26
  • Sorry, but its not the same Commented Jul 23, 2019 at 18:31
  • 1
    No? That doesn't help at all or offer any insight into your issue? Error is pretty darn explicit. Asked yourself why no warning on user.id? Commented Jul 23, 2019 at 18:32

1 Answer 1

1

This error usually occurs when you try to access a property of an object that does not yet exist (usually because it is created async, with an API request for example). If this is your case, simply tell the template to wait for the object to exist with an v-if.

<template v-if="fields && fields.data">
  <tr v-for="user in fields.data" :key="user.id">
    <td>{{ user.id }}</td>
    <td>{{ user.name }}</td>
    <td>{{ user.email }}</td>
    <td>{{ user.password }}</td>
    <td>
      <a href="#" data-toggle="modal" data-target="#modal" @click="edit(user.id)">
            <span class="btn btn-primary">
                <i class="mdi mdi-edit"></i>
            </span>
      </a>
    </td>
  </tr>
</template>

Remember that you cannot set the v-if on the same tag as the v-for.

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

Comments

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.