Tips on handling a Vue computed property

I am struggling to dispatch an object that is created within a computed property in vue.js. Being fairly new to this framework, I can't seem to make it work.

My goal is to dispatch the object named "updateObject" to the vuex-store. I have tried using setters without success. I believe that if I can set the "varia" object to the same value as "updateObject", then maybe I could dispatch it? Any help would be greatly appreciated.

Below is the code snippet:

<template>
  <div class="detail">
     <b-row align-v="center"><b-button variant="success" @click="submit()">submit</b-button></b-row>
     // some more code...
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  data () {
    return {
      subID: '',
      res: '',
      showAlert: true,
      varia: null
    }
  },
  computed: {
    ...mapState([
      'FA',
      'Main',
      'Sub',
      'layouttype'
    ]),
    getVariable: function (Sub, layouttype) {
      const subID = this.layouttype.sub_id
      var filterObj = this.Sub.filter(function (e) {
        return e.sub_id === subID
      })
      console.log(filterObj)
      return filterObj
    },
    updateObject: {
      // getterfunction

      get: function () {
        var len = this.getVariable.length
        var res = []
        for (var i = 0; i < len; i++) {
          if (i in this.getVariable) {
            var val = this.getVariable[i].variable
            res.push(val)
          }
        }
        console.log(res)
        var ergebnis = {}

        res.forEach(key => {
          if (this.FA[key]) {
            ergebnis[key] = this.FA[key]
          }
        })

        return ergebnis
      },
      // setterfunction
      set: function (value) {
        this.varia = value
      }
    }
  },
  methods: {
    submit () {
      this.$store.dispatch('sendData', this.ergebnis)
    }
  }
}
</script>

It's indicating that "this.ergebnis" is undefined.

Answer №1

If you want to access "ergebnis" globally, you can declare it as a global variable under the data property like this:

export default {
  data () {
    return {
      subID: '',
      res: '',
      showAlert: true,
      varia: null,
      ergebnis : {}
    }
  },
  computed: {
    ...mapState([
      'FA',
      'Main',
      'Sub',
      'layouttype'
    ]),
    getVariable: function (Sub, layouttype) {
      const subID = this.layouttype.sub_id
      var filterObj = this.Sub.filter(function (e) {
        return e.sub_id === subID
      })
      console.log(filterObj)
      return filterObj
    },
    updateObject: {
      // getterfunction
      get: function () {
        var len = this.getVariable.length
        var res = []
        for (var i = 0; i < len; i++) {
          if (i in this.getVariable) {
            var val = this.getVariable[i].variable
            res.push(val)
          }
        }
        console.log(res)

        res.forEach(key => {
          if (this.FA[key]) {
            this.ergebnis[key] = this.FA[key]
          }
        })

        return this.ergebnis
      },
      // setterfunction
      set: function (value) {
        this.varia = value
      }
    }
  },
  methods: {
    submit () {
      this.$store.dispatch('sendData', this.ergebnis)
    }
  }
}

By following these steps, you can now access the "ergebnis" variable throughout your code.

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

Unable to locate the root element for mounting the component in Cypress with React

I am currently testing my react app, which was created using create-react-app, with the help of cypress. Unfortunately, I encountered an error that looks like this: https://i.stack.imgur.com/xlwbo.png The error seems to be related to trying to fetch an ...

The checkbox will be automatically checked whenever there is any modification in the textbox

I'm currently working on a Grid view with a checkbox and two textboxes. What I want is for the checkbox to be automatically checked whenever there is a change in one of the textbox values, for example switching from 0 to 1. This project is being devel ...

Why is the view not reflecting the updates made to the model?

I recently started delving into Angular JS and have been following tutorials to learn it. One issue I'm facing is that my model doesn't seem to update the view. Can anyone help me figure out why this is happening? Here is the code snippet: < ...

Distributing utility functions universally throughout the entire React application

Is there a way to create global functions in React that can be imported into one file and shared across all pages? Currently, I have to import helper.tsx into each individual file where I want to use them. For example, the helper.tsx file exports functio ...

Tips for concealing a tab upon selecting an option from a dropdown menu

<ul class="nav nav-tabs"> <li class="active"><a data-toggle="tab" href="#song">Song</a></li> <li id="image-tab"><a href="#image" data-toggle="tab">Image</a></li> </ul> <div class="tab-cont ...

Using Vuetify to highlight selected radio buttons in an edit form

One issue I am encountering involves editing a table row. After clicking on the edit button, a form pops up with the data pre-filled for editing purposes. However, the radio button selected previously does not display as checked in the form; both options a ...

What is the best way to create a more compact Select component in React?

Working on React's Select component has been quite the challenge for me. I'm in the process of creating a simple dropdown button, also known as a ComboBox, similar to what can be seen on GitHub Insights: https://i.stack.imgur.com/J9Bcd.png Belo ...

JavaScript nested function scope loss issue is being faced

Could someone provide an explanation of the scope binding in this code snippet? window.name = "window"; object = { name: "object", method: function() { nestedMethod: function() { console.log(this.name); ...

Leverage the camera functionality in both native and web applications using Ionic/AngularJS and Cordova

Could you provide some guidance on how to use the Camera feature in both web and native environments? I have tried implementing it using the code snippet below, taken from ng-cordova documentation: $scope.takePicture = function() { var options ...

Utilizing CSS for styling a class with a dynamic name

On the server side, I am dynamically generating class names like this: <p class="level_1">List item 1</p> <p class="level_2">List item 2</p> <p class="level_3">List item 3</p> <p class="level_1">List item 1</p& ...

An error was encountered in the JSON syntax: Unexpected symbol <

I have encountered a problem with my code that I cannot seem to resolve. Despite the JSON data being successfully sent to the backend and processed correctly, the success function of the call is never triggered. Here is the snippet of my function: Regist ...

How to Retrieve Information from an Array in VueJS

At the moment, the data being displayed on my page is an array, as shown in the snippet below: https://i.stack.imgur.com/zAvrc.png However, I only want to retrieve or display the project names. This is all I have at the moment: fetch(context,id){ ...

What is the best way to fetch and convert information from an API to display on a website?

I am encountering an issue while trying to retrieve data from an API. Below is my code with a fabricated access code. $(function () { var $data = ('#data'); $.ajax({ type: 'GET', url: 'http://api.openweathe ...

Encountering an issue with Angular 5.2 application build on VSTS build server: Running into "CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory" error

Out of nowhere, the builds began failing with the following error : 2019-01-03T12:57:22.2223175Z EXEC : FATAL error : CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory error MSB3073: The command "node node_modules/webpack/bin/w ...

An error message indicating that the page is currently being unloaded has appeared

While working on a NodeJS-ReactJS Isomorphic App, I encountered an issue when clicking on a Link. An error message popped up saying: Uncaught (in promise) Error: Request has been terminated Possible causes: the network is offline, Origin is not allowed by ...

Using a Javascript method to access a sibling property within an object

Is there a way to access a sibling property from a method in JavaScript? This seemingly simple task has proven challenging for me. Take a look at the sample code below. let f = { a: 3, printMyBrother() { console.log(X) } }.printMyBrother f() ...

AngularJS is encountering an issue where it cannot locate the App Module

Whenever I attempt to execute this code, I encounter errors. Below is the code followed by the error message: index.html: <!DOCTYPE html> <html> <head> <meta id="meta" name="viewport" content="width=device-width, initial-scale=1 ...

Sending URL parameters from a React frontend to an Express API call involves crafting the request URL with

I'm currently working on a project where I need to make a fetch request from React to Express. The API URL requires parameters, but I want the last part of the URL to be dynamic. How can I pass a parameter from React to the Express API URL? Here' ...

Display the results from the API in a div using Vue.js

Currently working on implementing dynamic buttons to fetch data from an API call. Struggling with pushing the data array to the template and div. Here is my VueJS setup: var example = new Vue({ el: '#example', data: function () { ...

Can someone help me understand why this AJAX script is returning a status code of 404?

After getting involved with AJAX and utilizing the MAMP web server, I decided to work on a simple script to test its functionality. My attempt led me to investigate using the status property of XMLHttpRequest(); As it turns out, the issue I encountered was ...