Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: commit is not a function #108

Open
oliviermattei opened this issue Jan 27, 2022 · 1 comment
Open

TypeError: commit is not a function #108

oliviermattei opened this issue Jan 27, 2022 · 1 comment

Comments

@oliviermattei
Copy link

oliviermattei commented Jan 27, 2022

Hello, when i'm trying to use the store I have this error

proxy.js?fe19:354 Uncaught (in promise) TypeError: commit is not a function

here my code

import { action, createModule, mutation } from 'vuex-class-component';
import { Country } from '@/interfaces/country';
import { CountriesAdapter } from '@/adapters/countries.adapter';
import axios from 'axios';

const VuexModule = createModule({
  namespaced: 'Countries',
  strict: false,
});

export class CountriesStore extends VuexModule {
  private countries: Country[] = [];

  @mutation
  clear() {
    this.countries = [];
  }

  @mutation
  setCountries(payload: Country[]) {
    this.countries = payload;
  }

  @action
  async getCountries(forceRefresh = false) {
    const { data } = await axios.get(`${process.env.VUE_APP_COUNTRIES_API_URL}/countries`);

    const response = CountriesAdapter.get(data);

    this.setCountries(response);
    return response;
  }

  // Explicitly define a vuex getter using class getters.
  get getCountriesList() {
    return this.countries;
  }
}

And in my component

 mounted() {
    this.$store.dispatch('Countries/getCountries');
  }

I don't where is the problem.

@michael-scheurer
Copy link

michael-scheurer commented Mar 10, 2022

Solved the exact same problem by not accessing class member variables or class methods directly from within an action but instead via a commit() from inside an action method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants