Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!