The Vuex getters are throwing back undefined values

Check out my code snippet from store.js :

export const store = new Vuex.Store({
    state: {
        staffID: '',
        count: 0,
    },
    getters:{
        getStaffID: state => {
            console.log("13 getStaffID: " + state.staffID)
            return state.staffID;
        }
    },
    mutations: {
        UPDATE_STAFFID: (state,value) => {
            state.staffID = value
            console.log("20 mutations: " + state.staffID)
        },
    },
    actions: {
        update_staffID: (context, payload) => {
            context.commit("UPDATE_STAFFID", payload)
        }
    }
  })

In one of my components, there's a button that triggers the following:

this.$store.commit('UPDATE_STAFFID','miow')
console.log("store.getStaffID: " + this.$store.getStaffID);
console.log("store.staffID: " + this.$store.staffID);

The log results in:

20 mutations: miow
13 getStaffID: miow
store.getStaffID: undefined
store.staffID: undefined

This situation is puzzling. Based on the log, I can deduce that:

  • The mutation UPDATE_STAFFID executes successfully
  • state.staffID inside the getStaffID getter in store.js correctly displays the value as miow
  • However, for some reason, the getter returns undefined
  • Attempting to directly access the staffID value using this.$store.staffID also results in undefined

What could be causing these issues with undefined values?

Answer №1

Don't forget to include the getters and state properties by adding them in this way:

 console.log("Accessing store.getStaffID: " + this.$store.getters.getStaffID);
 console.log("Retrieving store.staffID: " + this.$store.state.staffID);

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

Validation of Email Existence (Using Django and Javascript/Ajax)

I'm currently facing a challenge with building an E-mail validator using Django and Javascript/Ajax. Despite my efforts, I seem to be stuck at a certain point where the Ajax response consistently shows: {response: "This field is required.", email: fa ...

Discovering precise information on an external website

I am currently attempting to determine if a list of websites contains specific content using Javascript and a Node.js server. I am open to using JQuery if necessary, but I am unsure of the best approach. For instance, I want to know if websites like "www.s ...

Implementing route navigation in two components using a click button

To display the content of the Quiz component with the path "/quiz" after clicking a button and fetching the component with the path "/" is my goal. Here is the code snippet from the App.jsx file: <Router> <Routes> <Route ...

Setting the aspect ratio in vue cropperjs can be easily achieved by following these steps

Does anyone know how to set the aspect ratio in the vue-cropper.js example? Unlike the jQuery version, I can't seem to find a way to do this through options. In the jQuery version, you could pass options to the element where the crop is used, but I&ap ...

Listen for events emitted by a child component in Vue.js 2.0 using the vm.$on method

After going through the vue.js events section on events, I've noticed that it mainly provides examples of how to listen to events using the vm.$on handler within HTML. With the new changes for 2.0, I'm not sure how to smoothly transmit an event f ...

The XML data seems to be missing from the HTML page

I've been working on a project where I need to display XML data on an HTML page. To accomplish this, I'm using Django to generate the content in the XML file and Javascript to periodically check for new posts and load them onto the page. Below i ...

Display/hide the entire div element

I am currently working on a project with 2 divs, where I want to display only one div at a time and hide the other. For example, if div 1 is visible, then div 2 should be hidden. You can check out what I have done so far in this demo. Everything is workin ...

What is the reason for my result showing as Object { } rather than MyObj { }?

When utilizing the web development tools console, if a browser object is typed, it will return as "console." > console Console { } > console.log(console) undefined > Console { } This behavior applies to all browser objects. However, when I try ...

What is the reason behind lightbox not disabling scrolling?

While using the default lightbox2 CSS and JavaScript, everything is functioning correctly except for an issue on Safari mobile. When I click an image and the lightbox opens, swiping to the left reveals blank white space despite having overflow-x set to hid ...

When attempting to send a file using fetch with formData to a Node.js server, the result shows that req

I have always included files with form data using the action attribute and multipart enctypes in my HTML forms. However, recently I had to utilize fetch to send a form and utilized new FormData() which was able to read all the fields and files from a speci ...

Mastering the Proper Use of Bootstrap-Tagsinput in Angular2

I have been trying to incorporate the bootstrap-tagsinput library into my Angular2 project. I successfully installed the library using the package.json file: "dependencies": { ... "bootstrap-tagsinput": "^0.7.1", ... } After installation, ...

`initMap` does not exist as a function

Hey everyone, I need some help: I recently integrated the Google Maps API into my AngularJs project and encountered an error in the console: Uncaught message: "initMap is not a function" name: "InvalidValueError" This occurs when the Google Map API is ...

Prevent sluggish script performance using GreaseMonkey

When I go to a certain website, there is an external script that loads very slowly, causing the page to be almost unusable at times. While disabling the script helps a bit, it means losing out on some important functionality. I'm wondering if there i ...

Utilizing JQuery to recycle data post-load

I've got this function: // AJAX MESSAGES DISPLAYING show_msg.on('click', function(e){ var $this = $(this), url = $this.attr('href'), url_info = url + ' .basic_info > *', url_msg = url + ' .cont ...

JavaScript: A timer that relies solely on the concept of TIME

Hello all, I have a specific question regarding starting a timer in JavaScript for a test scenario. Despite researching on my own and seeking help on various platforms, I haven't found a solution that fits my requirements. I am looking to implement a ...

What is the best way to extract this function from the methods section of this Vue component?

I recently came across this Vue component called BarChart.vue on https://github.com/apexcharts/vue3-apexcharts Here is the script section of the component; <script> /* eslint-disable */ export default { name: "BarExample", data: funct ...

How can Vue FullCalendar be configured dynamically?

I am trying to dynamically change some options in Vue FullCalendar, but the calendar is not applying them. You can check out my example here. The button at the bottom should set the maxTime option from 20:00 to 18:00. Is there a way to refresh the calen ...

Is there a way to showcase interactive HTML content similar to an ePub or eBook without the need to convert the HTML into ePub

Looking to enhance the mobile reading experience with a responsive design similar to popular ebook readers like Kindle or iBooks? Want to break long articles into full-screen sections for easy navigation on small devices? Consider using dynamic HTML to ada ...

Could one potentially generate new static files in Nextjs without needing to rebuild the entire app?

After recently beginning to utilize NextJs' getStaticProps feature, I have found that the static files generated at build time are quite impressive. However, my content is not static and requires updates without having to rebuild the entire app each t ...

Using bootstrap with a navbar in an asp.net mvc application proves to be challenging

There are only a few bootstraps that work with this site, such as the default and lumen bootstrap. You can find them on However, other bootstraps like materia and flatly seem to be causing display issues. Here's an example of how it looks: Edit: Bel ...