0

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'). 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. 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: {
  }
})

1 Answer 1

1

Look at your initial fruits 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' })

5
  • Thanks. It works now. If I wanted to use what I put in the input field to add to the array what should I write then? (instead of banana each time). addPlants(state, fruit) { state.fruits.push( {msg: fruit}) }, does nothing
    – Micha
    Commented Sep 12, 2022 at 10:21
  • @Micha If you want to add a certain fruit to the list (e.g. Banana), add it as a new parameter, just like you do with setQuery. addPlants(state, fruit) { state.fruits.push({ msg: fruit }) } Commented Sep 12, 2022 at 10:23
  • Unfortantly this gives me the same error VueJS TypeError: Cannot read properties of undefined (reading 'toLowerCase') but the state.fruits.push({ msg: 'Banana' }) does work so i accepted the answer you gave.
    – Micha
    Commented Sep 12, 2022 at 10:29
  • @Micha I suspect this is because you did not update the way you call the updated addPlants method. Note that it has a new parameter, and calling it the old way will just push { msg: undefined } into the array. A quick and dirty solution for it would be to update the button action to @click="() => addPlants('Banana')". The proper way would be to create a new method in your SearchComponent component and handle it there. Commented Sep 12, 2022 at 10:36
  • btw. to catch these bugs easier in the future, check out devtools.vuejs.org Commented Sep 12, 2022 at 10:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.