2

I want to check the inputs for emptiness when clicking on the button I filter the array if one of the inputs is empty I try to add an error to the array, but when I click on the button I get the error "'ErrorList' of undefined" I think that the error is that I trying to get an array called ErrorList inside a method called save, but how do I get rid of this problem then? You can also look at my code in codesandbox

<template>
  <div>
    <form>
      <div v-for="(learning, i) in general.learnings" :key="i">
        <input type="text" v-model="general.learnings[i]" maxlength="120" />
      </div>

      <button @click="save">Save</button>
    </form>
  </div>
</template>

<script>
export default {
  methods: {
    save(e) {
      e.preventDefault();

      this.general.learnings.filter(function (el) {
        if (el !== "") {
          return true;
        } else {
          this.errorList.push("Error");
        }
      });
    },
  },
  data() {
    return {
      errorList: [],
      general: {
        learnings: ["", ""],
      },
    };
  },
};
</script>
1
  • 1
    Try out to use an arrow function to get access to this like this.general.learnings.filter( (el)=> { Commented Aug 24, 2021 at 19:41

1 Answer 1

3

this is happening because reference of this is not referencing to your component inside the filter.

Simply change to arrow function to fix the issue.

 this.general.learnings.filter( (el) => {
        if (el !== "") {
          return true;
        } else {
          this.errorList.push("Error");
        }
      });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.