Vue.js is experiencing an issue with undefined values within computed properties

I have a specific input tag with the model selectedProp:

<input type="text" v-model="selectedProp" />

and I need to loop through items in this manner:

<div v-for="item of filteredItems">{{item.prop}}</div> 

Below is the code snippet for the component:

export default {
  name: 'App',
  data() {
    return {
      items: [],
      selectedProp: "",
      projects: [],
      errors: []
    }
  },
  created() {
   axios.get(`${URL}`)
   .then(response => {
      // JSON responses are automatically parsed.
      this.items = response.data;
    })
    .catch(e => {
      this.errors.push(e)
    });

  },

  computed: {
    filteredItems() {
      if(this.selectedProp) {
        console.log(this.selectedProp);
        return this.items.filter(function (item) {
          return item.prop == this.selectedProp;
        });

      }
      return this.items;

    }
  },
}

An Issue

The value of 'this' is not defined within the computed property

Answer №1

To tackle this situation, an arrow function can be employed as it has the capability to access the this object

 return this.items.filter( (item)=> {
      return item.prop == this.selectedProp;
    })

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 registration form's captcha did not prevent the submission of incorrect captchas

This is a PHP register.php page <?php require_once 'config.php'; ?> <?php if(!empty($_POST)){ try { $user_obj = new Cl_User(); $data = $user_obj->registration( $_POST ); if($data)$succ ...

Unable to receive data from jQuery AJAX request

I'm feeling a little puzzled at the moment. Whenever I run my ajax call, the error function is triggered every time. I am aware that the data is returning as JSON, and I have set the datatype as jsonp to enable cross-origin functionality. I am not sur ...

Creating reusable components with Vue and Pinia

I have a plan to develop a versatile component named Table, which will incorporate two other components - Search and Records. To facilitate this, I am intending to establish a pinia module that utilizes a state variable for entering search criteria. The se ...

An error occurred when trying to read the 'insertBefore' property of null, triggering an uncaught TypeError in the promise

<div class="button" @click="display = !display"> <i class="fas fa-plus-circle" v-if="display"></i> <i class="fas fa-minus-circle" v-else></i> </div> After clicking t ...

What is the best way to display a unique image in a div based on the size of the

Being new to web design, I have a question about creating a webpage with a large image in the center like on GitHub's Windows page. How can I work with the image inside that particular div or area based on different screen sizes? Is it possible to mak ...

Can the parameters in a .slice() be customized within a v-for loop?

I am currently working with Laravel 8 and using blade syntax. The following code snippet is from a Vue component I created: <li class="w-3/12 md:w-auto pt-0 md:px-4 md:pt-2 md:pb-0 list-none relative" v-if="comic.item_type === 'b&ap ...

What is the best way to upload my React project to GitHub without adding the node modules directory?

I'm looking to share my React Project on GitHub, but I don't want to include the node modules folder. What's the best way to go about this? ...

The significance of my values fluctuates within the datepicker, at times acknowledged and other times

Here's the situation in a nutshell: <script> //I decided to make things easier for myself by hard coding these values var startDate = '01-01-2013'; var endDate = '01-31-2013'; $(document).ready(function() { $('#R ...

Using Java beans to present form data utilizing session

How can I utilize Java Beans and session beans to store user input data and display a preview of the entered details on the next page? I have already created a servlet using JSP, but now I want to incorporate Java Beans to showcase the form data. How shoul ...

Deactivate all functions of a complete row in the table

I'm working with a table that contains various user interaction controls in its cells, such as buttons and links. I'm looking to gray out certain rows of the table so that all operations related to those rows are disabled. Does anyone have sugge ...

Slower CSS rendering as Javascript finishes its tasks

I am currently dealing with a jQuery plugin that is quite large and complex, but I will not be sharing it here to keep things simple. The issue I am facing is relatively straightforward, so I will focus on the essential code snippets: There is a click eve ...

What is the ideal project layout for a React and Node.js application?

With all the latest trends in the JS world, it can be a bit overwhelming to figure out the best way to organize files and folders for a project. While there are some examples from Facebook on GitHub, they tend to be quite basic. You can also check out som ...

Generate a fresh JSON object following a click event triggered by an HTTP PUT request

I have the following structure in JSON format: "disputes": [ { id: "", negotiation_type: "", history:{ user_flag: "", created_at: "", updated_at: "", created_by: null, updated_by: null, ...

What is the process for importing the required dependencies for the maps module in react-svg-map?

Exploring interactive maps led me to discover the project react-svg-map. As I followed the example provided, a certain issue caught my attention: import Taiwan from "@svg-maps/taiwan"; The developers mentioned that they have separated the map&ap ...

Ways to showcase title using ng-repeat

<input type="text" ng-model= "name" title="{{name}}">//this title displays the name that is contained in ng-model Similarly... <select title ="{{item.actionId}}" ng-model="item.actionId"> <option ng-repeat="action in actionList" value="{{ ...

Get selectize.js to display only options that begin with the user's input

Using selectize.js, my current setup looks like this: https://i.sstatic.net/EqhF8.png Instead of only showing words that start with 'arm', it displays words or options containing 'arm' as a substring elsewhere. I want to modify the f ...

Step-by-step guide on building a factory in Angular for a pre-existing service

Currently, I am delving into the world of angularjs and exploring articles on service and factory functionalities. One particular example that caught my attention is showcased in this ARTICLE, which includes a demonstration using a Service Fiddle. As I de ...

Vue 3 tutorial: How to implement v-model calculations with Axios in a web application

Currently, I am delving into the world of CRUD with Laravel 9, Vue 3, and axios. My current project involves number calculations using v-model. While the code is working fine, I have encountered a significant issue. I managed to successfully calculate &ap ...

Guide to displaying a local HTML file in CKEditor 4.3 using jQuery

Using CKEditor 4.3 on my HTML page, I aim to import content from a local HTML file and display it within the editor. (My apologies for any English errors as I am Brazilian :P) The jQuery code I have tried is as follows: $(document).ready(function(){ ...

Guidelines on launching an ionic 4 modal using routes

How can I open a modal using routes? I attempted the following approach, but it did not work as expected: ngOnInit() { this.launchModal(); } async launchModal() { const modal = await this.modalController.create({ component: AuthPasswordR ...