Attempting to trigger an action from a Vuex module proves futile as the error message "[vuex] unknown action type" is generated

I've been struggling to successfully register a Vuex module. Below is the code I've been working with:

stores/index.js:

import Vuex from 'vuex';
import resourcesModule from './resources';

import axios from '@/helpers/axiosInterceptor';

Vue.use(Vuex);


export default new Vuex.Store({
  modules: {
    resources: resourcesModule
  },
  state: {...}})

stores/resources/index.js


export default {
    state: {
        resources: []
    },
    actions: {
        fetchResources(store) {
            const request = axios.get('/api/musical_resources');
            request.then((res) => {
                store.commit('setResources', res.data);;
            }); 
            return request;
        }
    },
    mutations: {
        setResources(state, payload) {
            state.resources = payload;
        }
    },
    getters: {
        resources({resources}) {
            return resources;
        }
    }
};

I've attempted to dispatch the fetchResources action from another component, but I keep getting an error stating " [vuex] unknown action type: fetchResources". Upon inspecting the store object, I noticed that only the root property is present under the _modules property, which leads me to believe that I may have missed something during the module registration process.

When I console.log(resourcesModules); in stores/index.js, everything appears to be in order.

Can anyone help me pinpoint the issue? Thank you.

EDIT:

I also attempted the following in stores/index.js:

import Vue from 'vue';
import Vuex from 'vuex';

import axios from '@/helpers/axiosInterceptor';

Vue.use(Vuex);


export default new Vuex.Store({
  modules: {
    resources: {
      state() {
        return {
          resources: []
        }
      },
      actions: {
        fetchResources(store) {
          const request = axios.get('/api/musical_resources');
          request.then((res) => {
            store.commit('setResources', res.data);;
          });
          return request;
        }
      },
      mutations: {
        setResources(state, payload) {
          state.resources = payload;
        }
      },
      getters: {
        resources({ resources }) {
          return resources;
        }
      }
    }
  },
  state: {...}})

Unfortunately, this attempt was also unsuccessful.

Answer №1

Here is a suggestion:

export default new Vuex.Store({
  modules: {
    resourcesModule
  },
  state: {...}})

for utilizing the fetchResources method

Alternatively,

export default new Vuex.Store({
  modules: {
    resources: resourcesModule
  },
  state: {...}})

for invoking resources.fetchResources

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

Can you explain the distinction between angular-highcharts and highcharts-angular?

Our team has been utilizing both angular-highcharts and highcharts-angular in various projects. It appears that one functions as a directive while the other serves as a wrapper. I'm seeking clarification on the distinctions between the two and recomme ...

What is the most effective method for achieving a desired outcome?

Is it a valid approach to get an action result, and if so, how can this be achieved? For instance, if there is a page with a form for creating entities, after successfully creating an entity, the user should be redirected to the entity's detail view. ...

Conceal content after a specific duration and display an alternative in its position, continuously repeating the loop

Imagine creating a captivating performance that unfolds right before your eyes. Picture this: as soon as the page loads, the initial text gracefully fades away after just three seconds. In its place, a mesmerizing animation reveals the second text, which ...

I am looking to create a div that can consistently refresh on its own without needing to refresh

I have implemented a comment system using ajax that is functioning well. I am looking to incorporate an ajax/js code to automatically refresh my "time ago" div every 5 seconds so that the time updates while users are viewing the same post. Important note: ...

Employing getters in the toObject() method

As I delve into the code of a Node.js Express application for learning purposes, I came across the following line that sparked my curiosity regarding the inclusion of getters and virtuals. var pgmsDbObj = chnnlList[chnnlIndex] var pgmsObj = pgmsDbObj.to ...

Running the Express service and Angular 6 app concurrently

Currently, I am in the process of developing a CRUD application using Angular6 with MSSQL. I have managed to retrieve data from my local database and set up the necessary routes, but I am encountering difficulties when it comes to displaying the data on th ...

Combining Two Objects in Laravel and Vue.js: A Step-by-Step Guide

Currently, I am in the process of developing a basic CRUD App using Laravel along with Vue.JS (including Vuex). However, I have encountered a small issue. The problem lies within the 'categories' table, the structure of which is visible in the pr ...

Exploring the Worldwide Influence of TypeScript, React, and Material-UI

I am currently following an official tutorial on creating a global theme for my app. In my root component, I am setting up the global theme like this: const themeInstance = { backgroundColor: 'cadetblue' } render ( <ThemeProvider theme ...

Loop through each item in the JSON object

After making an ajax call, I receive a json object in the success callback with the following structure: {test 1: Array(2), test 2: Array(3), test 3: Array(2)} The arrays inside each key have elements that look like this: 0: {IdProduct: "1", ProductName ...

I need to update a JSON file on the server by adding a new object to the existing array using the fs.appendFile function

I have a JSON stored on the server like this: [{"a":1}, {"a":2}] and I'm wondering if there is a way to add an object at the end without having to rewrite the entire file on the server. As a workaround, I have been omitting the brackets and adding the ...

Drop-down options disappear upon refreshing the page

Code snippet for sending value to server using AJAX in JavaScript In my script, the status value may vary for each vulnerable name. When selecting a status option and storing it in the database through AJAX, the selected value is lost after refreshing th ...

Is there a jQuery or Javascript alternative to CSS Counter?

Can a counter be implemented that changes the text of a tag directly using jQuery/Javascript? For example, if there were two tags like this: <a>hello</a> <a>bye</a> After executing the jQuery/JS function, the result would be: < ...

Protractor Error: Identifier Unexpectedly Not Found

I've encountered a slight issue with importing and exporting files while working on Protractor tests. HomePage.js export default class HomePage { constructor() { this.path = 'http://automationpractice.com/index.php'; this.searchQ ...

Utilizing React to highlight buttons that share the same index value upon hover

I have some data in a JavaScript object from a JSON file, where certain entries have a spanid (number) while others do not. I've written React code to highlight buttons with a spanid on hover, but I'm looking for a way to highlight or change the ...

Exploring the functionality of CodePen's code editor in relation to developing a 2D shooting game

Recently, I created a straightforward 2D shooter game with all the code neatly organized in a single HTML file: (file_gist). When I tested the game in my chrome browser, everything worked flawlessly according to my intentions. However, upon transferring th ...

When making an ajax call, I passed the data "itemShape" but on the URL page, an error appeared stating "Undefined index: itemShape"

Hello, I have been using an Ajax function to send data with the key itemShape. However, when I directly access the URL page or service page, it displays the following error: Notice: Undefined index: itemShape in C:\wamp64\www\inventory_so ...

Having trouble with Vue.js image force download not functioning properly with anchor tags?

Currently, I am utilizing Laravel in conjunction with Vue.js. I have had the requirement to forcibly download an image on the browser, but unfortunately, it is not functioning as expected. var link = document.createElement('a'); link.href = &apo ...

Can you demonstrate how to repeatedly increase a running total using a for loop until the loop ends?

Looking at the for loop. The goal is to have the interest calculated based on the latest value variable. Currently, the script generates the same interest rate and value for each year. What I desire is for it to use the previous value variable to determine ...

Tips for extracting an XML value from an API

I'm attempting to showcase a value retrieved from an API (specifically used by PRTG software to extract information). Here is the link to the API: The API provides an XML file structured like this: <?xml version="1.0" encoding="UTF-8"?> <ch ...

Tips for utilizing the onclick method to modify an ejs variable

, I am currently in the process of developing a web application utilizing Node, Express, and ejs. Specifically, I have created a page that features a series of buttons (displayed in the first table) alongside an associated set of hidden divs (shown in the ...