0

I'm completely new to frontend development and Vue js. I want to involve a state managment in my application. I can set the state with the response from my API but when I want to filter the results in getter using filter function (getActiveLeagues) I get an error that 'filter is not a function' as if the result was not an array but I think I declared my state with as an array:

const state = {
  leagues: [],
};

const getters = {
  getAllLeagues: (state) => state.leagues,
  getActiveLeagues: (state) => state.leagues.filter((league) => league.archivated === false),
};

const actions = {

  async getAllLeagues({ commit }) {
    const response = await axios.get(`${server.baseURL}/api/league`);
    console.log(response);
    commit('setLeagues', response);
  },
};

const mutations = {
  setLeagues(state, leagues) {
    state.leagues = leagues;
  },
};

export default {
  state,
  getters,
  actions,
  mutations,
};

and I get an array from API:

enter image description here

Here is how my Vuex.Store call look like:

import Vue from 'vue';
import Vuex from 'vuex';
import posts from './modules/postModule';
import leagues from './modules/leagueModule';

Vue.use(Vuex);

export default new Vuex.Store({
  modules: {
    posts,
    leagues,
  },
});

And the result of state watch: enter image description here enter image description here Probably it's a stupid problem cause i'm new to this but I can't see what is wrong at the moment. Thanks for any help.

3
  • 1
    What does your new Vuex.Store constructor call look like? Are you passing state to it? Commented May 28, 2020 at 13:15
  • Edit: I uploaded it. I'm passing two modules with state is it right? Commented May 28, 2020 at 13:29
  • If you try to debug/log your state.leagues, what is the result? Commented May 28, 2020 at 13:35

1 Answer 1

1

I actually haven't used modules and am not sure how it works compared to flat state. Best thing is to console.log(state) inside the getter to get a better view.

One thing I notice, compared to the docs, is you're not declaring state as a function in the module. E.g.:

const state = () => ({
  leagues: [],
});

Reference: https://vuex.vuejs.org/guide/modules.html

EDIT (in response to your new screenshot)

It looks like state.leagues.leagues what you're looking for (thanks to module namespacing I guess?), but even that is not an array, it's an object with data property that is an array.

So start with this:

commit('setLeagues', response.data);

I think your module state should be receiving it's namespaced substate already, so you shouldn't have to modify anything else. But if it's still broken, try this as well, I'm curious:

getActiveLeagues: (state) => state.leagues.leagues.filter((league) => league.archivated === false),
Sign up to request clarification or add additional context in comments.

6 Comments

State can be either a function which returns an object or an object literal. It doesn't really make a difference there, although I do agree he should still console/debug the state.
@CristianoSoleti oh OK, I would still call plain object a bad habit, since reusing modules could lead to mutated initial state. Just like in Vue itself. vuex.vuejs.org/guide/modules.html#module-reuse - but yeah, not looking to be the actual problem here
Edit: I posted what I get in Vue plugin and console.log. It's actually the mentioned error :/ Ok, anyway I try with state as a function in a moment
@Pavello see my edit, I think you need to store response.data in the commit. response itself isn't the array
Sorry for a little late. Yes! That was it, i works fine with this solution: commit('setLeagues', response.data); Thanks very much guys
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.