Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

371
Vistas
Error Alpine Expression Error: Cannot read properties of undefined when deleting data from key/value pair in console

I receive the following errors when using the removeData function in the code below.

Alpine Expression Error: Cannot read properties of undefined (reading 'name')

Expression: "data[id].name"

<input x-model="data[id].name" type="text" name="name"></input>

Alpine Expression Error: Cannot read properties of undefined (reading 'text')

Expression: "data[id].text"

<input x-model="data[id].text" type="text" name="text"></input>

Everything appears to work correctly but the error message is generated in the console. Is there a way to stop this from happening?

function handler() {
  return {
    data: {},
    getData() {
      console.log(this.data);
    },
    addData() {
      x = Object.keys(this.data);
      x = x.map(function(x) {
        return parseInt(x, 10);
      }).sort();
      n = x[x.length - 1];
      if (isNaN(n)) {
        n = 0;
      };
      this.data[n + 1] = {
        'name': '',
        'text': ''
      }
    },
    removeData(id) {
      delete this.data[id];
    },
  }
}
<script src="//unpkg.com/alpinejs" defer></script>

<div x-data="handler()">
  <button type="button" @click="getData()">ShowData</button>
  <button type="button" @click="addData()">AddData</button>
  <template x-for="[id, value] in Object.entries(data)" :key="id">
    <div :id="id">
      <input x-model="id" type="number">
      <input x-model="data[id].name" type="text" name="name">
      <input x-model="data[id].text" type="text" name="text">
      <button type="button" @click="removeData(id)">Remove</button>
    </div>
  </template>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you correct the following, you will find the cause.

this.data[n + 1] = {
//+
        'name': `${n+1}taro`,
        'text': `${n+1}taro san shibakariheitta`
      }
    },

//+
  <pre x-text="JSON.stringify(data, null, 2)"></pre>

How about the following as a solution?

//+
      <input x-model="value.name" type="text" name="name">
      <input x-model="value.text" type="text" name="text">
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda