Using Vuex: how to access the modules variable within an action

In my Vue application, I have a global store structured as follows:

// Note: These Stores Are Modularised, please refer to: [https://vuex.vuejs.org/guide/modules.html] for more information.
const store = new Vuex.Store({
  modules: {
    surfers: surfers,
    surfSites: surfSites,
    surfTickets: surfTickets
  },
  actions: {
    resetAllState: ({ dispatch, modules }) => {
      console.log(modules); // Undefined
      console.log(store.modules); // Undefined
      console.log(this.modules); // Error in v-on handler: "TypeError: _this is undefined"
      for (const currentModule in modules) {
        console.log(`Resetting Module State: ${module}`);
        if (modules[currentModule].state.hasOwnProperty("initialState")) {
          dispatch("resetModuleState", currentModule);
        }
      }
    },
    resetModuleState: (currentModule) => {
      console.log(`Resetting Module State: ${currentModule}`);
    }
  }
});

The purpose of these actions is to loop through the modules and trigger a reset state action when a user logs out.

However, I am facing issues where modules, store.modules, and this.modules are all either undefined or causing an error related to being undefined...

Can anyone provide guidance on how to dynamically access modules in this manner, if possible?

Answer №1

There seems to be an issue with the code provided. Actions are only allowed access to

{ commit, dispatch, getters } as parameters

However, it appears that you have tried to pass modules in the parameters. You can still access the modules using the following approach:

Insert this code inside the "resetAllState" action

resetAllState: function ({ dispatch }) {
  for (const currentModule in modules) {
    this._modules.root.forEachChild((childModule) => {
      console.log(childModule);
    });
    //if (modules[currentModule].state.hasOwnProperty("initialState")) {
    //  dispatch("resetModuleState", currentModule);
    //}
  }
},

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

The Ajax response fails to update my viewmodel

I have a value and a list that I need to update from within an Ajax callback. After retrieving a fresh value using .get(), I try to assign it to my view model's property, but the UI does not refresh properly. Below is the code snippet: function Searc ...

Do we really need to implement ajax in this code snippet?

In my scenario, I have two JSP files named test1.jsp and test2.jsp. The flow of my program goes like this: I need to retrieve data from a textbox in test1.jsp, but the Ajax call is initiated from a different page. My objective is to receive the controller ...

What is the best way to convert a locale code into a language name with i18next?

In the application I'm working on, there is a language selector feature that allows users to choose the display language for the app. Currently, we only have limited options available, but there are plans to expand the list in the future. My goal is t ...

Why do Computed Setters fail to refresh the View?

In the current view, there are two buttons available for selection: one for the list layout and the other for the group layout. The chosen state needs to be stored either in local storage or a cookie. // Pug Syntax #app span.button( :class="{'a ...

Ways to implement the tabIndex attribute in JSX

As per the guidelines provided in the react documentation, this code snippet is expected to function properly. <div tabIndex="0"></div> However, upon testing it myself, I encountered an issue where the input was not working as intended and ...

What is the best way to transfer information to a different NextJS page?

I want to implement a search input field. The idea is to allow the user to type something into the search bar and then send that input to another page where an API request will be made with the search content. Essentially, when the user types "something" ...

Service's read-only attribute

I am in need of a cache service to store data efficiently. angular.module('core').service('markerCache', function(){ var cache = []; this.set_cache = function(arr){ cache = arr; this.cached = true; }; th ...

Is there a way to prevent the context menu from appearing when tapping on an image on a mobile phone?

When attempting to move an image on the screen with a touch event, a popup appears in Chrome when pressing and holding the image. This popup usually includes options to save the image. Is there a way to disable this popup so I can freely move the image w ...

I encountered an issue while attempting to download a zipped folder using an AXIOS get request, as the folder appeared

I have developed an application in NodeJs for the backend and VueJS for the frontend. I am attempting to download a zip file that is generated within the app on the frontend using the code snippet below: const downloadZIP = () => { AXIOS_REQUEST( ...

Unable to save object in JavaScript memory

Currently, I am in the process of implementing a Stack in JavaScript using a LinkedList. However, I encountered an issue when trying to instantiate a Node class. When attempting to create a variable let newNode = new Node(x), I am receiving undefined. I a ...

Issue with Bootstrap 5 Dropdown not toggling back

<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Creative Admin Panel</title> <link rel="stylesheet" href=&q ...

Tips for signaling to an AngularJS form that the input value has been altered

I developed a sign up form using angularjs. The submission process is functioning correctly. Now, I want to implement validation to check if the email provided is already registered. If it exists, display an error message indicating that the email is alrea ...

Angular form displayed on the screen

I'm having trouble finding a solution to this issue, as the form data is not being output. var app = angular.module('myApp', []); app.controller('mainController', ['$scope', function($scope) { $scope.update = funct ...

Altering the href text within a script causes the text to disappear instead of updating

Function Triggered by Button Click: function LoadEnglishText() { document.getElementById("txt_whatwedo_learnmore2").innerHTML = "here."; } HTML Link to be Updated: <a id="txt_whatwedo_learnmore2" href="./pdf/Pricing_App_Dev_2019_Ger.pdf">hier ...

Ensure that the alert for an Ajax JSON record count remains active when the count is

Trying out Ajax JSON for the first time has been a bit tricky. Even though I hard coded "Record: 1" on the server side, it keeps alerting me with a total record of 0. I'm not sure where I went wrong. Could it be an issue with how I passed the array da ...

Determine the total row count retrieved from a HTML table using Javascript

Currently, I have an HTML table that is being searched using JavaScript. I am looking to determine the number of rows that are remaining after the search and then display that information to the user. As a beginner, I would like some guidance on how to m ...

Why is the "class" attribute of an SVG node not being applied when I change it?

I am having trouble changing the "class" attribute of a node in SVG using my AngularJS Directive. Even though I've written the code to do so, it doesn't seem to be applied properly. This is the code snippet from my Directive: node = node.data(f ...

Dynamically generate Nav List Items in Vue.js based on the paths of route children

I am attempting to create a dynamic navigation list using the routes defined in my routes.js: const routes = [ { path: '/', component: () => import('layouts/MainLayout.vue'), children: [ { ...

The instance is referencing property or method "foo" during render, but it has not been defined. To ensure reactivity, please make sure that this property is defined

Just starting out with the Vue framework and experimenting with component composition. However, I'm encountering an issue when trying to run the code: "Property or method "icons" is not defined on the instance but referenced during render. Make sure ...

Passing data from a parent node to a child node using JavaScript and the D3.js library

Creating a legend (var legend) for my d3js chart involves binding data to the parent 'g' element, specifically a string (label) that is needed in the child 'text' element. Any ideas on how to assign the parent data from the 'g&apo ...