Error: When trying to import a store from a JavaScript module, an uncaught TypeError occurs because the property 'getters' is unable to be read

I've been attempting to import a vuex store into a custom JavaScript module within my Vuex application. Despite trying various methods, the build shows 100% compliance each time. However, I keep encountering the following error in the browser console, with nothing being loaded:

Uncaught TypeError: Cannot read property 'getters' of undefined

I have experimented with different ways of importing it, all resulting in the same error response.

import store from '../store'
import { store } from '../store'
import store from '../store/index.js'

Additionally, I've attempted to access the store using different approaches such as:

let tokens = store.state.user.tokens
let tokens = store.getters['user/getTokens']

The structure of my store file is as follows:

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

import user from './modules/user'

Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    user
  }
})

export default store

Below is the code snippet for the store/modules/user.js file..

export default {
  namespaced: true,
  state: {
    user: {},
    active: false,
    tokens: {},
  },

  mutations: {
    updateUser(state, user) {
      state.user = user.details
      state.active = true
      state.tokens = user.tokens
    },
  },
  actions: {
    async updateUser({ commit }) {
      try {
        const res = await axios().get('/user/details') //axios details omitted here
        commit('updateUser', res.data.user)
      } catch (error) {
        console.log(error)
      }
    },
  },
}

I'm puzzled by this issue. While I have successfully implemented the process in another project, it seems to be failing in this particular instance. The application does not load at all and only returns this error. Any assistance on this matter would be greatly appreciated.

Answer №1

It's hard to provide a precise diagnosis without seeing more of your code. I recently set up a sample Vuex module, store, and Vue component, and it seems to be functioning correctly.

user-store-module.js

const userModule = {
  state: () => ({
    users: [
      {
        id: 1,
        name: 'Tim'
      },
      {
        id: 2,
        name: 'Debbie'
      }
    ]
  }),
}

export default userModule;

store.js

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

import userModule from './user-store-module.js'

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    userModule: userModule
  }
})

VuexModule.js (Vue 2 with Vue CLI)

<template>
  <div class="vuex-module">
    <h3>Users</h3>
    <div class="row">
      <div class="col-md-6">
        <table class="table table-bordered">
          <thead>
            <tr>
              <th>ID</th>
              <th>NAME</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="user in users" :key="user.id">
              <td>{{ user.id }}</td>
              <td>{{ user.name }}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    computed: {
      users() {
        return this.$store.state.userModule.users;
      }
    },
  }
</script>

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

Placing a div with position:absolute inside another div with position:absolute and turning them into position:fixed

HTML <div class="box1"></div> <div class="box2"> <div class="box3"></div> </div> CSS Properties of box1, box2 and box3 are: box1 { position: absolute; z-index: 100; background-color: rgba(39, 39, 39, 0.3) ...

The jQuery AJAX response consistently comes back empty

Hello, I'm currently working on creating an HTML form and I need to validate it before submitting the form action. However, when I use AJAX to respond, I keep receiving a blank message. Can anyone help me with this issue? $(function(){ $("#ajax-p ...

The PUT method in Axios and vue-resource seems to be malfunctioning

My front-end is developed using Vue.js and the back-end uses Java. Within a single component, I retrieve user data from the database. The Users.vue file contains the following: getUsers() { this.$http.get("/user") .then((response) => { thi ...

Vue3 - Managing the Enabling and Disabling of Text Input and Checkbox Based on Input Number

<div class="container-body" v-for="index in 10" :key="index"> <div class="container-content"></div> <div class="container-content"> <input :id="'Row&apo ...

What impact does reselect have on the visual presentation of components?

I'm struggling to grasp how reselect can decrease a component's rendering. Let me illustrate an example without reselect: const getListOfSomething = (state) => ( state.first.list[state.second.activeRecord] ); const mapStateToProps = (state ...

I'm struggling to make this background show up in a div

Anyone able to help me figure this out? I can't seem to get the achtergrond_homepage.png as a background in rounded corners. Edit: It seems like the gray color is always on top. Could it be controlled in the JavaScript part? This is the CSS: @ch ...

transferring information to a different application module (or page) through a double-click feature in AngularJS

Within my angularjs application, I am faced with a challenge involving two pages. The first page retrieves data from a database and displays it in a table. Upon double-clicking on a row, the user is directed to another page where more detailed informatio ...

Starting expressjs on shared A2hosting is a simple and quick process

I've been searching extensively, but I can't seem to get my express js app to start. All I'm encountering is a 404 error. The default app.js file with an http server works perfectly fine: var http = require('http'); var server = ...

Refactor these codes by utilizing a single loop in React

Can you help me with reducing the amount of code in this function? The code provided below is functional and does not have any issues. flag : boolean, likeArr: ["A", "B", "C"], dislikeArr: ["D", "E", "F"] likeOrDislike( flag, likeArr, dislikeArr ) { ...

Techniques for retrieving elements from HTML using jQuery

I'm developing a feature that allows users to add videos to playlists by clicking on a link in a popover. The challenge I'm facing is extracting the video_id and the selected playlist when the user interacts with the popover. Any tips on how to a ...

Exploring Ways to Modify a .txt File with Javascript or jQuery Forms

Looking for a way to access a .txt file offline in the browser and display its data in different form fields for each line. Any tutorials available for this? ...

Steps for inserting a clickable phone number link within innerHTML1. First, create an

I've been trying to include a specific phone number using the <a href> tag within the innerHTML. I attempted using both double and single quotes. In the first case, nothing happens at all. In the second case, although the phone number appears, ...

Creating a UI Component when clicking on something

Currently, I am immersed in a project and had a question regarding the possibility of rendering UI components on click. In other words, is it feasible to trigger component rendering through an onclick event? For instance, consider the following example: & ...

Using Javascript to retrieve the selected value from a dropdown menu does not return any results

Struggling to retrieve the selected value from a drop-down list using JavaScript? Even when an item is chosen, do you keep getting 'null' as the result? Let's take a look at the HTML page: <select class="mySelect"> <option val ...

When a model.find is passed as an argument to be invoked, it results in an error

After working with ExpressJS for a while, I decided to explore using Mongoose alongside it. In the callback of my queries where I handle errors like this: function( error, data ) {...} , I found myself repeating code. To streamline this process, I created ...

Prop type failure: The `actions` prop is specified as mandatory in the `Testing` component, however, its value is currently undefined

I am working on a project that involves creating a login form using React and Redux. Here's a snippet of my app.js: import React from 'react'; import { render } from 'react-dom'; import Input from 'react-toolbox/lib/input&apo ...

Is there a way to properly handle the loading of all images within a specific jQuery selection?

After running the code below, it is executed for each item in the selection: $('.someElement img').load( function() { // Do something }); Consequently, if .someElement has 5 images, it triggers 5 separate times. How can I set up an event so ...

Transforming control of execution seamlessly between two interactive functions in nodejs

Two functions are used to take input from the CLI using process.stdin. The issue arises when one function is done taking input, as a similar function is called. However, while the control shifts to the second function, the first function continues executin ...

What is the best way to eliminate the selected text while moving/grabbing an element?

I'm facing an issue with removing the background text image when dragging elements. I have tried troubleshooting but haven't found a solution yet. For reference, I looked at this link: HTML5 Drag and Drop anywhere on the screen function drag_sta ...

Jquery code failing to trigger any response

Recently, I quickly created a jQuery script to dynamically populate a paragraph element in order to easily switch between player and server interaction options. However, I am currently facing an issue where my script does not populate as expected. I have a ...