Issue with Vuex getters not properly returning data

I am encountering an issue with a Vuex getter that is supposed to return the state of courseData. Despite the getter being present and having values when I check it using console.log(this.$store.getters), I am unable to access the data. When I try to use

console.log(this.$store.getters.getCourseData)
or
console.log(this.$store.getters['courses/getCourseData'])
, it unexpectedly returns an empty array instead of the expected object.

Component:

<script>
import { mapActions, mapGetters } from 'vuex'

export default {
  data() {
    return {
      data: null,
    }
  },
  methods: {
    ...mapActions('courses', ['fetchCourseData']),
  },
  computed: {
    ...mapGetters('courses', ['getCourseData'])
  },
  created() {
    this.fetchCourseData(this.$route.params.code) // <-- This line sets the state
    console.log(this.$store.getters) // <-- List of store getters
    console.log(this.$store.getters['courses/getCourseData']) // <-- Returns empty array
    console.log(this.$store.getters.getCourseData) // <-- Returns undefined
  }
}
</script>

Store:

import axios from 'axios'

export default {
  namespaced: true,
  state: {
    courseData: [],
  },
  getters: {
    getCourseData(state) {
      return state.courseData
    }
  },
  actions: {
    async fetchCourseData({ commit }, code) {
      await axios
        .get('teacher/course-management/course/code/' + code)
        .then(response => {
          commit('SET_COURSE_DATA', response.data.data)
        })
    }
  },
  mutations: {
    SET_COURSE_DATA(state, courseData) {
      state.courseData = courseData
    }
  },
}

Answer №1

Utilize the async/await feature not only in your action but also in the created method.

async created() {
    await this.fetchCourseData(this.$route.params.code) // <-- This line initializes the state
    console.log(this.$store.getters) // <-- Displaying list of store getters
    console.log(this.$store.getters['courses/getCourseData']) // <-- Results in an empty array
    console.log(this.$store.getters.getCourseData) // <-- Returns as undefined
  }

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

What is the best way to set the v-model property to an object that is constantly changing

I'm in the process of creating a dynamic form that allows users to add additional fields by simply clicking on a button labeled "adicionar condição." The concept is similar to what can be seen in the screenshot below: https://i.stack.imgur.com/Mpmr6 ...

Can someone explain why v-for is unable to display the computed value?

When selecting a city and area, the data should be filtered accordingly. However, I am facing an issue where the selected value does not appear. I have tried various solutions and searched for similar code, but I have yet to find a resolution. Below is a ...

"Troubleshooting: PrimeVue DataTable Not Displaying Proper

Attempting to utilize PrimeVue and implement the DataTable Component, but it is not appearing. It seems to be an issue related to $slots error? The Button component is rendering and functioning as expected. Table.vue <template> <div> ...

Customizing the Zoom Control Style in Vue Leaflet

I'm currently working on developing a Map App in Dark Mode using Vue, but I've encountered an issue with changing the style of the Zoom Control. Here's what I have tried so far: template> <div class="main-map"> <l ...

Is it possible to host an SSR nuxt.js application on GitHub or GitLab pages?

Can you host a server-side rendering application using nuxt.js (universal ssr mode) with a Firebase backend on GitHub/Gitlab Pages? Gitlab provides an example at nuxt, and I'm curious if the server-side dynamic fetching still functions properly. ...

v-tooltip specifically designed for the append-icon

I'm currently working with Vuetify and facing an issue with applying a tooltip only for the append-icon in a v-text-field. The current problem is that the tooltip does not work for icons at all! View code on CodePen <v-tooltip bottom> < ...

The vuex module has replaced the state field "foo" with another field of the same name located at "foo"

There is a warning showing up in the console that says: [vuex] state field "foo" was replaced by another module with the same name at "foo" Can you explain what this warning indicates and how I may have made an error? ...

Implementing vue.js to insert a new user into the user array stored in a JSON file

I'm encountering an issue with vue.js where I am unable to successfully add a new user to the user array. Despite console.logging "this.user" when calling the function on submit, I am not seeing a new box with the new user or the new user being added ...

What is the significance of having both nulls in vue's ref<HTMLButtonElement | null>(null)?

Can you explain the significance of these null values in a vue ref? const submitButton = ref<HTMLButtonElement | null>(null); ...

Begin fetching data asynchronously in Vue Nuxt to initialize data

I'm working on a Vue-Nuxt component and I need to set the initial value of a data property in my component using data retrieved from an asyncData API call. data () { return { selected_company: this.contracts.company1.id *--> this gives me an ...

What is the method for incorporating a global function with a plugin to display notifications in Vue.js?

Recently, I encountered an issue while trying to create a global function using a plugin. Everything seemed to be working fine until I faced difficulties in displaying my notifications. Tired of repeating the notification display methods everywhere in my c ...

Issues with running Vue commands have been reported in Git-Bash, however they seem to work fine in

Whenever I check the version of Vue in my Terminus bash by running vue --version, here's the output I receive: $ vue -v /bin/sh: /Users/kirkb/AppData/Local/Yarn/bin/../Data/global/node_modules/.bin/vue: No such file or directory In PowerShell, when I ...

Creating a Loop with v-for in Laravel Framework that works similarly to the Forelse in Laravel

Trying to achieve a similar functionality to forelse in Laravel framework blade using Vue. This is just a test to check if a table has records or not, and if not, display a default value: <tr> <td colspan="4">There's No Records Yet< ...

The scrolling event on the div is not triggering

I'm struggling with this piece of code: const listElm = document.querySelector('#infinite-list'); listElm.addEventListener('scroll', e => { if(listElm.scrollTop + listElm.clientHeight >= listElm.scrollHeight) { th ...

Optimal method for centrally aligning a v-card both horizontally and vertically using Vuetify

I'm currently working on the login page for my website and I'm having trouble centering my v-card both horizontally and vertically. The code I have right now is as follows: <template> <v-layout align-center justify-center column fill ...

Tips for managing large amounts of data retrieval from an API

As a beginner, I am attempting to retrieve data from an API and display it using the v-for directive. However, this process is causing my app to lag. It freezes when new data is fetched or when I search within the list. The following code snippet shows whe ...

Parts are tuned in to information within the main component

Visit this link I am trying to control the text color of "box" elements with a checkbox. <div id="app"> <label><input type='checkbox' v-model='showBlack' />Show black</label> <box>Hello</box& ...

Tips for circumventing the use of responsive tables in Buefy

I am facing a challenge with displaying data in a Buefy table in a way that it appears as a conventional table with columns arranged horizontally on all devices, rather than the stacked cards layout on mobile. In order to accommodate the content appropriat ...

Tips for generating a node for the activator attribute within Vuetify?

Vuetify offers the 'activator' prop in multiple components like 'v-menu' and 'v-dialog', but there is limited information on how to create a node for it to function correctly. The documentation states: Designate a custom act ...

What could be causing my Vuetify Stepper Component to be hidden?

I am utilizing Vue and Axios to display data using the Stepper Component from Vuetify. However, after adding the API data, it seems that the CSS class is somehow being set to display: none;. I am confused as to why this is happening, so any help would be g ...