简体   繁体   English

VueJS TypeError:无法读取未定义的属性(读取'toLowerCase')

[英]VueJS TypeError: Cannot read properties of undefined (reading 'toLowerCase')

I can filter in a array and I delete items from this array.我可以过滤一个数组,然后从这个数组中删除项目。 My problem starts when I try to add items to this array list.当我尝试将项目添加到此数组列表时,我的问题就开始了。 I get the following error: TypeError: Cannot read properties of undefined (reading 'toLowerCase').我收到以下错误:TypeError: Cannot read properties of undefined (reading 'toLowerCase')。 I am not sure why I get this error, because when I use the add mutation I dont want the getter I used for my my filter mutation to even be used.我不确定为什么会出现此错误,因为当我使用 add 突变时,我不希望使用我用于过滤突变的 getter。 Can someone explain to my what this error means and how I can fix it?有人可以向我解释这个错误的含义以及如何解决它吗?

This is my component code:这是我的组件代码:

<template>
    <div id="app">
      <div>
          <input type="text" v-model="query" placeholder="Search plants..." />
        <div class="item fruit" v-for="fruit in filteredList" :key="fruit.msg">
            <p>{{ fruit.msg }}</p> 
            <button @click="deletePlants(index)">
            Delete task
          </button>
        </div>
      </div>
      <div class="item error" v-if="query && !filteredList.length">
          <p>No results found!</p>
      </div>
      <input v-model="fruits">
      <button @click="addPlants">
          New plant
        </button>

    </div>
  </template>
  
  <script>
    import { mapMutations, mapGetters } from 'vuex'
    
  export default {
    name: 'SearchComponent',
    props: {
        msg: String
    },

    computed: {

        ...mapGetters([
      'filteredList'
      // ...
    ]),
        query: {
            set (value) {
                this.setQuery(value);
            },

            get () {
                return this.$store.state.query;
            }
        }

        
      },

      methods: {
        ...mapMutations([
             'setQuery',
             'addPlants',
             'deletePlants',
             'setPlants'
             ]),

        
      }
  };
  </script>

<style>

And this is the code in my store file:这是我的商店文件中的代码:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    strict: true,
  state: {
        query: '',
        fruits: [
          { msg: 'Monstera'},
          { msg: 'Aloe vera'},
          { msg: 'Bonsai'},
          { msg: 'Cactus'},
          { msg: 'Bananenplant'},
          { msg: 'Ficus'},
          { msg: 'Calathea'},
        ]

  },

  mutations: {

    setQuery(state, value ) {
        state.query = value;
    },

    addPlants(state) {
        state.fruits.push('Banana')
    }, 

    deletePlants (state, index){
        state.fruits.splice(index, 1);
    },
  },

  getters: {

  filteredList (state) {
    return state.fruits.filter((item) => {
        return item.msg.toLowerCase().indexOf(state.query.toLowerCase()) !== -1
        })
  }

  },
  actions: {
  },
  modules: {
  }
})

Look at your initial fruits state:看看你最初的果实 state:

fruits: [
          { msg: 'Monstera'},
          { msg: 'Aloe vera'},
          { msg: 'Bonsai'},
          { msg: 'Cactus'},
          { msg: 'Bananenplant'},
          { msg: 'Ficus'},
          { msg: 'Calathea'},
        ]

Then at the way you add new fruits to this array:然后在向这个数组中添加新水果的方式:

state.fruits.push('Banana')

You will eventually end up with something like:你最终会得到类似的东西:

fruits: [
          { msg: 'Monstera'},
          { msg: 'Aloe vera'},
          { msg: 'Bonsai'},
          { msg: 'Cactus'},
          { msg: 'Bananenplant'},
          { msg: 'Ficus'},
          { msg: 'Calathea'},
          'Banana',
        ]

To fix this, update your addPlants to state.fruits.push({ msg: 'Banana' })要解决此问题,请将您的addPlants更新为state.fruits.push({ msg: 'Banana' })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取未定义的属性(读取“toLowerCase”)(在 setup.js 上) - TypeError: Cannot read properties of undefined (reading 'toLowerCase') ( on setup.js ) 无法读取未定义的属性(读取“toLowerCase”) - Cannot read properties of undefined (reading 'toLowerCase') 类型错误:无法在 vuejs 中读取未定义的属性(读取“添加”) - TypeError: Cannot read properties of undefined (reading 'add') in vuejs TypeError:无法读取未定义的属性(读取“集合”)-Vuejs 和 Firebase - TypeError: Cannot read properties of undefined (reading 'collection') - Vuejs and Firebase TypeError:无法使用 VueJS 读取未定义的属性(读取 '$refs') - TypeError: Cannot read properties of undefined (reading '$refs') with VueJS vuejs 2 组合 api: TypeError: Cannot read properties of undefined (reading 'length') - vuejs 2 composition api: TypeError: Cannot read properties of undefined (reading 'length') Jest Vuejs 测试 - 类型错误:无法读取未定义的属性(读取“参数”) - Jest Vuejs Test - TypeError: Cannot read properties of undefined (reading 'params') 类型错误:无法读取未定义的属性(读取“$router”)vuejs - TypeError: Cannot read properties of undefined (reading '$router') vuejs 如何修复此错误:“类型错误:无法读取 Discordjs 中未定义的属性(正在读取‘toLowerCase’)”? - How do I fix this error: "TypeError: Cannot read properties of undefined (reading 'toLowerCase')" in Discordjs? React MultiSelect 组件显示`无法读取未定义的属性(读取'toLowerCase') - React MultiSelect component showing` Cannot read properties of undefined (reading 'toLowerCase')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM