最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VueJS getting TypeError: Cannot read properties of undefined when I delete and add an Sensor information - Stack Ov

programmeradmin0浏览0评论

I am trying to create sensorArray dynamically in my VueJS application. For this when I create an element and delete it and then when I try to create a new one then I get the following error:

client.js:227 TypeError: Cannot read properties of undefined (reading 'value')
    at eval (templateLoader.js?!./node_modules/vue-loader/lib/index.js?!./pages/Test.vue?vue&type=template&id=cde3d4aa&:44)
    at Proxy.renderList (vue.runtime.esm.js:2643)

Following is the simple Vuejs application I have:

<template>
  <div>
    <span>Sensor Information : </span>
    <button class="btn btn-info" @click="addSensorInfo($event)">
      Add Sensor
    </button>
    <br>
    <span v-if="sensorArray.length > 0"><b>Sensor Element: </b></span>
    <span v-if="sensorArray.length > 0">&nbsp;&nbsp;
      <div v-for="sensor in sensorArray" :key="sensor.ID" class="form-group">
        <label class="form-label">Value</label>&nbsp;&nbsp;
        <input v-model="sensorArray[sensor.ID].value" type="text" class="form-control">
        <button class="btn btn-danger" @click="deleteSensorInfo($event,sensor.ID)"><i class="bi bi-trash" /></button>
      </div>
    </span>
  </div>
</template>

<script>
export default {
  data () {
    return {
      sensorID: 0,
      sensorArray: []
    }
  },
  methods: {
    addSensorInfo (event) {
      event.preventDefault()
      const sensor = {}
      sensor.ID = this.sensorID
      this.sensorArray.push(sensor)
      this.sensorID++
    },
    deleteSensorInfo (event, sensorID) {
      event.preventDefault()
      this.sensorArray.splice(this.sensorArray.filter(obj => obj.ID === sensorID), 1)
    }
  }
}
</script>
  1. Click on Add Sensor button: A text field and delete button will appear.
  2. Click on Delete ICON and delete the field.
  3. Now again click on Add Sensor then I get following error:
client.js:227 TypeError: Cannot read properties of undefined (reading 'value')

Since I have a lot of elements within my SensorArray I am not creating the dedicated element and trying to create everything dynamically based on user click. Can someone please let me know how can I fix this issue?

I am trying to create sensorArray dynamically in my VueJS application. For this when I create an element and delete it and then when I try to create a new one then I get the following error:

client.js:227 TypeError: Cannot read properties of undefined (reading 'value')
    at eval (templateLoader.js?!./node_modules/vue-loader/lib/index.js?!./pages/Test.vue?vue&type=template&id=cde3d4aa&:44)
    at Proxy.renderList (vue.runtime.esm.js:2643)

Following is the simple Vuejs application I have:

<template>
  <div>
    <span>Sensor Information : </span>
    <button class="btn btn-info" @click="addSensorInfo($event)">
      Add Sensor
    </button>
    <br>
    <span v-if="sensorArray.length > 0"><b>Sensor Element: </b></span>
    <span v-if="sensorArray.length > 0">&nbsp;&nbsp;
      <div v-for="sensor in sensorArray" :key="sensor.ID" class="form-group">
        <label class="form-label">Value</label>&nbsp;&nbsp;
        <input v-model="sensorArray[sensor.ID].value" type="text" class="form-control">
        <button class="btn btn-danger" @click="deleteSensorInfo($event,sensor.ID)"><i class="bi bi-trash" /></button>
      </div>
    </span>
  </div>
</template>

<script>
export default {
  data () {
    return {
      sensorID: 0,
      sensorArray: []
    }
  },
  methods: {
    addSensorInfo (event) {
      event.preventDefault()
      const sensor = {}
      sensor.ID = this.sensorID
      this.sensorArray.push(sensor)
      this.sensorID++
    },
    deleteSensorInfo (event, sensorID) {
      event.preventDefault()
      this.sensorArray.splice(this.sensorArray.filter(obj => obj.ID === sensorID), 1)
    }
  }
}
</script>
  1. Click on Add Sensor button: A text field and delete button will appear.
  2. Click on Delete ICON and delete the field.
  3. Now again click on Add Sensor then I get following error:
client.js:227 TypeError: Cannot read properties of undefined (reading 'value')

Since I have a lot of elements within my SensorArray I am not creating the dedicated element and trying to create everything dynamically based on user click. Can someone please let me know how can I fix this issue?

Share Improve this question edited Sep 27, 2021 at 8:43 kissu 47k16 gold badges90 silver badges189 bronze badges asked Sep 27, 2021 at 8:36 BATMAN_2008BATMAN_2008 3,5809 gold badges52 silver badges161 bronze badges 7
  • You could use .prevent modifier as shown here: vuejs/v2/api/#v-on You are also probably in this case: vuejs/v2/guide/reactivity.html#For-Arrays Did you checked what you do have in your Vue devtools after the delete? – kissu Commented Sep 27, 2021 at 8:42
  • @kissu Thanks for your response. You mean event.preventDefault() right? If so then I am already using it and still getting errors. Can you please suggest me something? – BATMAN_2008 Commented Sep 27, 2021 at 9:29
  • Just remended a small improvement here. It will not fix the other issues. Did you checked your Vue devtools? What is the current state after the deletion? – kissu Commented Sep 27, 2021 at 9:30
  • @kissu After the delete operation my sensorArray bees sensorArray:Array[0] and sensorID:1 so basically that added element has been removed during the deleteoperation. Now, when I try to add again then i get the error. – BATMAN_2008 Commented Sep 27, 2021 at 9:39
  • Where is the issue located actually? Is it this.sensorID++ or with the this.sensorArray.push(sensor)? Maybe try this.sensorID += 1 here. Otherwise, I guess that a minimal reproducible example could be helpful here! – kissu Commented Sep 27, 2021 at 9:44
 |  Show 2 more ments

2 Answers 2

Reset to default 2

First Error

This is not a valid JavaScript operation

this.sensorArray.splice(this.sensorArray.filter(obj => obj.ID === sensorID), 1)

Splice signature expects a number at first position

splice(start: number, deleteCount: number)

Filter signature returns an array.

Solution

const idx = this.sensorArray.findIndex(obj => obj.ID === sensorID);
if (idx !== -1) {
    this.sensorArray.splice(idx, 1)
}

Or as mentioned you can use a more concise form

this.sensorArray = this.sensorArray.filter(obj => obj.ID === sensorID);

Second Error

Now I noticed that you are performing a strange array access. An array is indexed by id not by your custom sensor.ID, so this sensorArray[sensor.ID].value makes no sense. It works before a delete by pure chance (due to how you are generating IDs).

This should be your code (note the v-model="sensor.value"):

<div v-for="sensor in sensorArray" :key="sensor.ID" class="form-group">
    <label class="form-label">Value</label>&nbsp;&nbsp;
    <input v-model="sensor.value" type="text" class="form-control">
    <button class="btn btn-danger" @click="deleteSensorInfo($event,sensor.ID)"><i class="bi bi-trash" /></button>
</div>

Furthermore .value is never set.

You are mixing up splice and filter, to delete the item. Simply use filter.

this.sensorArray = this.sensorArray.filter(obj => obj.ID === sensorID);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论