Store returns undefined when accessed outside of Component



  • Dear Communitty

    I am in the process of writing my business logic in plain JS files, and would like to access my store to retrieve data stored in the current state. However, I am unable to access the state and am returned a “TypeError: Cannot read property ‘chartOfAccounts’ of undefined”. It thus appears as though it is not the “store” that is undefined, but rather the ‘lumpsum’ module of my store. My question is therefore: How can I access the data from Vuex in my business logic outside of a component?

    My folder structure is the following:

    └── store
        └── lumpsum
              └── state.js
               ...
     
    

    This is the plain JS File:

    import store from '../store/lumpsum/index'
    
    let classifier = function () {
      // Import Data from Store
      let chartOfAccounts = store.state.lumpsum.chartOfAccounts
      console.log(chartOfAccounts)
    }
    
    export {
      classifier
    }
    
    

    In my component I am then importing this function, and have a method which calls this function.

    import { classifier } from 'src/utils/Classification'
    
    export default {
        methods: {
            classify: function () {
                classifier()
    } 
        }
    }
    

    My ‘store/lumpsum/state.js’ :

    export default function () {
      return {
        // Raw Data
        chartOfAccounts: []
    }
    

    Could anyone spot the issue which I am missing?

    Thank you in advance



  • @zerofix Did you find any solution to this issue?


Log in to reply