Vue alert: Issue in v-on function: 'TypeError: Unable to access property 'Array' of undefined

When I click on the button, I want to validate the inputs for empty values. I am attempting to filter the array and add an error to the array if any of the inputs are empty. However, upon clicking the button, I encounter the error message "'ErrorList' of undefined". It seems like the issue stems from trying to access an array named ErrorList within the save method. How can I resolve this problem? You can also view my code on 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>

Answer №1

One reason for this issue is that the reference of this is not pointing to your component within the filter.

To resolve this problem, simply switch to using an arrow function.

 this.general.learnings.filter( (el) => {
        if (el !== "") {
          return true;
        } else {
          this.errorList.push("Error");
        }
      });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The total sum of values within labels that share the same class

I am attempting to sum up all the values of class tmpcpa and store the result in final_cpa, but for some reason final_cpa always ends up being 0. document.getElementById('cpa' + arr[0]).innerHTML = cpa + '(' + '<label id="tmp ...

Is there a way to deactivate the previous button for today's date?

I need assistance with disabling the previous button on today's date in my calendar. It is crucial that users are only able to select the start date from today onwards. I am currently utilizing moment.js for my Datepicker. Any help would be greatly ap ...

Invoke a function within the React library through ReactDOM.render

Within my index.js file, I have the following code: const store = createStore( combineReducers({ i18n: i18nReducer }), applyMiddleware(thunk) ); syncTranslationWithStore(store) store.dispatch(loadTranslations(translationsObject)); stor ...

Axios mistakenly includes an additional trailing slash in body parameters

Currently, in the process of developing an application utilizing React Native, I am faced with the challenge of communicating with an IoT chip that possesses minimal RAM memory. Due to this constraint, all logic must be executed on the client side. One sp ...

Save the text found within an element to Appim wd.js

I am a beginner with Appium and I am currently exploring the workshop Git project, which utilizes wd.js and Grunt. From my research in the documentation and forums, I have learned that there are two methods for accessing the text of native elements within ...

Grouping a NumPy array by hour in a datetime column

Struggling in Python to group records from a csv by hour based on dates and times. Dealing with a file of around a million records, I've loaded it into a Pandas dataframe and structured a NumPy array with each record as a sublist. For example: #this ...

Displaying a blank column in a table using JavaScript

I am trying to write a JavaScript function that will add a new column to a table. I want this new column to be displayed at the index where it is added, and then hidden once the addition is complete. Here is the code for my method: function addColumnToTa ...

Preventing Page Refresh when Button is Clicked in Angular

How can I prevent page reload when a button is clicked in Angular? I'm developing a quiz application in Angular where I fetch random questions and options from an API. When users select an option, the next question should appear without reloading the ...

Partially extended texture in Three.js

Currently, I am using the Collada loader in Three.js r65 to load my 3D object. Upon loading, I apply a texture to all parts of the model using the following code snippet. var loader = new THREE.ColladaLoader(); loader.options.convertUpAxis = true; loader ...

Steps for generating a unique division element for every javascript response

How can I dynamically create a new div for each response in JavaScript? The message is in JSON format containing all the messages sent and received. This is my JavaScript code: $.get("MessageServlet", function (responseJson) { $.each(responseJ ...

Employing JavaScript to insert a JSON value as a parameter in an HTML element's attribute

In a JSON file, I have "img" properties with .jpg file paths as their values (e.g "images/image.jpg"). I am attempting to insert these values into the 'src' attribute of existing HTML elements. Below are my attempts so far: I tried creating te ...

Having trouble with accessing input field in a modal that was generated by TinyMCE React within a Next.JS environment

In my current project, I am utilizing Next.JS and looking to incorporate the TinyMCE editor onto my webpage. Here is the code snippet I have implemented: <TinyMceEditor selector='textarea' initialValue={ props.value } apiKey=<AP ...

Searching for a way to detect when a user clicks the back button on their Android or iPhone device using JavaScript or

In the process of building a Single Page Application (HTML5) utilizing the following plugins: - Sammy.js - Knockout.js - Require.js - Jquery.js This app is designed for Android, iPhone, and Windows mobile devices. Many scenarios revolve around cli ...

Only submit the form if all files are selected to prevent any missing files from being uploaded

I am currently utilizing the Vue.js framework along with Axios. Within my HTML page, I have incorporated two separate input fields for selecting an image. Additionally, there is a form present on the page. It's important to note that these inputs and ...

Jumping Iframe Anchor Link in Src

I'm facing a challenge with an iframe positioned in the center of a webpage. I want to show content from another page within the iframe, but not starting at the very top. To achieve this, I inserted an anchor into the src of my iframe, linked to an a ...

The React Callservice script is failing to fetch the required data from the Node.js script responsible for making the API call

Recently, I decided to create a basic webpage using React.js to display data fetched from an API. Although the project is intended to be straightforward, my lack of recent development experience has led to a perplexing issue that I can't seem to resol ...

Sending a value to a different Ionic page based on a specific condition

Here is the code snippet from my app.js: var app = angular.module('dbpjkApps', ['ionic']) app.controller('kategoriCtrl', function($scope) { $scope.listKat = [ {kat: 'math'}, {kat: 'physics'}, ...

Alter the row's color using the selection feature in the module

Is there a way to change the color of a row when a specific property has a certain value? I found a solution for this issue on Stack Overflow, which can be viewed here: How to color row on specific value in angular-ui-grid? Instead of changing the backgro ...

Turn numbers into asterisks

I have recently ventured into the world of programming and am currently working on a game involving two 6-sided dice. I want to use asterisks to represent the percentage of total rolls, but I'm having difficulty converting the numbers to print the app ...

The attached event in my Nuxt.js project fails to fire during testing with Jest

I'm currently in the process of developing a web application using nuxt js + jest Here is an excerpt from my nuxt js component: export default { mounted () { document.addEventListener('click', this.eventClick) }, destroyed () { ...