Vuex is throwing a mysterious ReferenceError that is leaving developers

Currently, I am developing a Single Page Application (SPA) using Vue.js and Vuex. Within this project, I have set up some data in the store and displayed it in a child component. This child component includes radio buttons that trigger a function called getCalculations when clicked. However, I am encountering an issue where I keep receiving an undefined error when trying to log the vehicle object within the getCalculations function. It is important to note that the child component is nested within a parent component.

Vuex Store

const getDefaultState = () => {
    return {
        //Vehicle Data
        vehicleData: {
            reg_no: "KAS 234R",
            chasis_number: "BGSHS-IUISUS",
            engine_number: "MNVSS-8787SNS"
        }   
    }
}

const state = getDefaultState()

//getters
const getters = {
    vehicle: (state) => state.vehicleData
}

//actions
const actions = {
    //......
}

//mutations 
const mutations = {
    // .....
}

export default {
    state,
    getters,
    actions,
    mutations
}

Parent Component

<template>
<div>
  <vehicleFeature/>
</div>
</template>

<script>
import { mapGetters } from "vuex";
import vehicleFeature from "./partials/vehicleFeature";

export default {
  name: "StepFour",
  data() {
    return {
        //.....
    };
  },
  computed: mapGetters(["vehicle"]),
  components:{
    vehicleFeature
  }
</script>

Child Component

<template>
<div>
       <form class="ipf_form">
            <div class="inputGroup">
                <input id="radio4" name="radio" @change="getcalculations" type="radio" value="4">
                <label for="radio4">1 INSTALMENTS</label>
            </div>
            <div class="inputGroup">
                <input id="radio5" name="radio" @change="getcalculations" type="radio" value="5" >
                <label for="radio5">2 INSTALMENTS</label>
            </div>
       </form>
</div>
</template>


<script>
import { mapGetters } from "vuex";

export default {
  name: "vehicleFeature",
  data() {
    return {
        //.....
    };
  },
  computed: {
      ...mapGetters(["vehicle"]),
      principalamount(){
        //.....
      }
    },
  methods: {
    getcalculations() {
        console.log(this.vehicle.reg_no);
      }
  }
</script>

<style scoped>

</style>

Answer №1

To update your code, simply make the following adjustments:

const state = {
    //Vehicle Information
    vehicleInfo: {
        reg_no: "KAS 234R",
        chasis_number: "BGSHS-IUISUS",
        engine_number: "MNVSS-8787SNS"
    }   
}

const getters = {
vehicleData: (state) => state.vehicleInfo
}

Then, to access the vehicle state use:

...mapGetters(['vehicleData'])
   this.vehicleData.reg_no

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

Pug template syntax for importing JavaScript files with links

My Programming Dilemma In my NodeJS webserver setup, I use Express and Pug to serve HTML and JavaScript files. Here's a snippet of how it looks: index.pug html head title Hello body h1 Hello World! script(src='s ...

Can we leverage map/filter/reduce functions within a promise by encapsulating the result with Promise.resolve()?

Currently, my approach to doing loops inside a promise looks like this: asyncFunc() .then(() => { return new Promise((resolve) => { for (let i = 0; i < length; i++) { // do something if (j == length - 1) { ...

Create a function that triggers a fade-out effect on one button when another button is clicked

Hello everyone! I'm still getting the hang of things around here so please be kind. I need some assistance with my weather app project. Specifically, I've created two buttons and I want to make it so that when one is clicked, the other fades to g ...

Is there a way to customize the Webpack output to incorporate specific options solely for a particular bundle?

Currently, I am using Webpack 4 to build multiple bundles. My requirement is to add the output options libraryTarget and library for a single bundle only. The default configuration looks like this: output: { path: path.resolve(__dirname, 'dist/j ...

Displaying PHP output within a JavaScript expression

Let's dive into a scenario involving a basic HTML document and some JavaScript that's loaded within it: <!-- doc.html --> <!doctype html> <html lang="en"> <head> <script type="text/javascript" src=" ...

Unable to locate the Shadcn error module: Error encountered when trying to resolve the path '@/components/ui/accordion' within the directory 'Desktop/sadcn/src'

I'm encountering an issue with my Shadcn setup in a simple React app with TypeScript. The error message I'm getting is: Module not found: Error: Can't resolve '@/components/ui/accordion' in '/home/vinayak/Desktop/sadcn/src&ap ...

Inserting an icon into a particular class

I am new to JavaScript and I am eager to understand the code I write rather than just using it. That's why I'm turning to this forum with a question regarding adding an icon to a colored group. Here is the code snippet I have: <strong> ...

Is it secure to store information that impacts component rendering within a JWT payload?

I am currently developing a MERN app where users have various roles, and different components are rendered based on their role in React. In my application, I store a JWT containing the user's ID and role in a cookie, which is then decoded in a global ...

As the height is expanded, the background color gradually infiltrates the body of the page

I am currently working on an angular application that utilizes angular-pdf. The controller and view function perfectly, and the PDF is displayed correctly, except for one issue. The height of the PDF exceeds the min-height of the module, causing it to expa ...

The socket.rooms value is updated before the disconnection listener finishes its process

When dealing with the "disconnect" listener, it's known that access to socket.rooms is restricted. However, I encountered an issue with my "disconnecting" listener. After making some modifications to my database within this callback, I attempted to em ...

What changes occurred to module file names following the process of minification?

I'm currently troubleshooting an issue with this particular code snippet: import globalComponents from './global-components'; // ... globalComponents.forEach((component) => { // eslint-disable-next-line no-underscore-da ...

Changing the state variable upon clicking to display varying components

Being new to React, I am exploring how to load different components based on state variables. I am looking for a way for my handler to dynamically determine which state variable I am updating without explicitly passing the state name as a string. List of ...

Tips on resolving the issue of an Axios post request not appearing in a get request in React

When using axios to make a post request in this code, a new username is posted, but there is an issue with retrieving the posted name from the API. How can I fix this problem to view my posted request? const App = () => { const [data, setData] = u ...

Ways to prompt the debugger to pause whenever a specific script file is called during execution in Edge/Chrome debugger

I am currently in the process of debugging a Typescript web application, which is quite new to me as I have never delved into web development before. This particular project entails multiple script files and various libraries. While running the applicatio ...

When executing `npm run serve`, the process becomes stuck at 40% during execution

I've been working on a Vue.js project with Vuetify for a few weeks now. I usually use the npm run serve command to build and run a live server, and everything was running smoothly. However, recently I encountered an issue where the project gets stuck ...

Creating session variables in Joomla using checkboxes and AJAX

I'm currently working on implementing session variables in Joomla with AJAX when checkboxes are selected. Below is the code snippet from select_thumb.ajax.php file: $_SESSION['ss'] = $value; $response = $_SESSION['ss']; echo ...

Manually input the date or choose from a calendar interface using Vuetify

As a newcomer to Vuetify, I have a question about the v-text-field and its integration with the v-date-picker. Is it possible to manually input the date as well as use the calendar for selection? I've noticed that when using the v-text-field with the ...

Scrolling to ID or Anchor using jQuery: Automatically scrolls to the top if already at the designated section

I've been on a quest to uncover the cause and solution for this issue. Lately, I've been utilizing $("#content").animate({scrollTop:$(#elementId).offset().top-183}, 600); to achieve smooth scrolling to an ID within a <div>. The number 18 ...

What could be causing my CORS fetch request to not send cookies to the server?

Trying to work out a CORS request using the fetch method: fetch('https://foobar.com/auth', { method: 'GET', mode: 'cors', credentials: 'include', }) The server-side code in express for impl ...

Prevent elements from displaying until Masonry has been properly set up

My goal is to merge Masonry elements with existing ones. Currently, the items appear before Masonry initializes then quickly adjust into position a moment later. I want them to remain hidden until they are in their proper place. This is the snippet (with ...