1

I have a simple lesson creator where I allow teachers to make selections for various categories, those selections ids are collected and grouped together and I want to return them all at the end as a lesson plan.

However, I am having a strange problem that I can't figure out. My Vuex store shows the selections correctly, however my getter duplicates all of my arrays.

After selections are made, my Vuex store shows something like this through the Vue.js dev-tools plugin:

lesson_store:Object
    lesson:Object
        selected_event:1
        selected_exploration:Array[1]
            0:1
        selected_extensions:Array[1]
            0:1
        selected_goals:Array[1]
            0:54
        selected_lexis:Array[1]
            0:2

store.js state and getter:

const state = {
    lesson: {
        selected_event: '',
        selected_exploration: [],
        selected_extensions: [],
        selected_goals: [],
        selected_lexis: [],
    }
};

getSelections(state) {
    console.log('GETTER SELECTIONS', state.lesson);
    return state.lesson
}

My call to getSelections from lesson.vue file:


<template><button @click="saveLesson">Save</button></template>


methods: {
    saveLesson () {
        console.log('GET RETURN OF SELECTIONS',this.$store.getters["getSelections"]);
    },
}

Now my console output is:

lesson_store:Object
    lesson:Object
        selected_event:1
        selected_exploration:Array[2]
            0:1
            0:1
        selected_extensions:Array[2]
            0:1
            0:1
        selected_goals:Array[2]
            0:54
            0:54
        selected_lexis:Array[2]
            0:2
            0:2

The thing is, none of my other getters behave this way. This getter is super basic. When I check out store and getSelections getter in the Vue.js dev-tools the values are correct and there are no duplicates.

Any advice or direction you can provide would be much appreciated.

UPDATE::::::

Actions and Mutations for Lesson_Store

// create mutations
const mutations = {
    setSelectedEvent(state, payload) {
        // state.lesson.selected_event = payload

        if (state.lesson.selected_event === payload) {

            state.lesson.selected_event = '';
        } else {
            state.lesson.selected_event = payload
        }

    },

    setSelectedReading(state, payload) {

        if (state.lesson.selected_reading === payload) {
            state.lesson.selected_reading = '';
        } else {
            state.lesson.selected_reading = payload
        }

    },

    setSelectedLexis(state, payload) {

        // if event is already in array, then remove it with filter
        // otherwise push it to the array
        if (state.lesson.selected_lexis.includes(payload)) {
            state.lesson.selected_lexis = state.lesson.selected_lexis.filter(function (item) {
                return item !== payload;
            });
        } else {
            state.lesson.selected_lexis.push(payload);
        }

        // state.lesson.selected_lexis = payload
    },

    setSelectedExplorations(state, payload) {

        // if event is already in array, then remove it with filter
        // otherwise push it to the array
        if (state.lesson.selected_exploration.includes(payload)) {
            state.lesson.selected_exploration = state.lesson.selected_exploration.filter(function (item) {
                return item !== payload;
            });
        } else {
            state.lesson.selected_exploration.push(payload);
        }

        // state.lesson.selected_lexis = payload
    },

    setSelectedQuestions(state, payload) {

        // if event is already in array, then remove it with filter
        // otherwise push it to the array
        if (state.lesson.selected_questions.includes(payload)) {
            state.lesson.selected_questions = state.lesson.selected_questions.filter(function (item) {
                return item !== payload;
            });
        } else {
            state.lesson.selected_questions.push(payload);
        }

        // state.lesson.selected_lexis = payload
    },

    setSelectedPerformances(state, payload) {

        // if event is already in array, then remove it with filter
        // otherwise push it to the array
        if (state.lesson.selected_performances.includes(payload)) {
            state.lesson.selected_performances = state.lesson.selected_performances.filter(function (item) {
                return item !== payload;
            });
        } else {
            state.lesson.selected_performances.push(payload);
        }

    },

    setSelectedExtensions(state, payload) {

        // if event is already in array, then remove it with filter
        // otherwise push it to the array
        if (state.lesson.selected_extensions.includes(payload)) {
            state.lesson.selected_extensions = state.lesson.selected_extensions.filter(function (item) {
                return item !== payload;
            });
        } else {
            state.lesson.selected_extensions.push(payload);
        }

    },

    setSelectedGoals(state, payload) {

        // if event is already in array, then remove it with filter
        // otherwise push it to the array
        if (state.lesson.selected_goals.includes(payload)) {
            state.lesson.selected_goals = state.lesson.selected_goals.filter(function (item) {
                return item !== payload;
            });
        } else {
            state.lesson.selected_goals.push(payload);
        }

    },


};

// create actions
const actions = {
    setSelectedEvent({commit}, payload) {
        commit('setSelectedEvent', payload);
    },

    setSelectedReading({commit}, payload) {
        commit('setSelectedReading', payload);
    },

    setSelectedLexis({commit}, payload) {
        commit('setSelectedLexis', payload);
    },

    setSelectedExplorations({commit}, payload) {
        commit('setSelectedExplorations', payload);
    },

    setSelectedQuestions({commit}, payload) {
        commit('setSelectedQuestions', payload);
    },

    setSelectedPerformances({commit}, payload) {
        commit('setSelectedPerformances', payload);
    },

    setSelectedExtensions({commit}, payload) {
        commit('setSelectedExtensions', payload);
    },

    setSelectedGoals({commit}, payload) {
        commit('setSelectedGoals', payload);
    },
};

All of these appear to be working correctly because my vuejs dev tools display all of the selection id's properly.

10
  • I am not sure if it matters but this lesson_store is in a module that is imported. Commented Sep 19, 2020 at 21:06
  • Where do you call saveLesson()? Commented Sep 19, 2020 at 21:06
  • I added a button in lesson.vue file <button @click="saveLesson">Save</button> I also tried to just load the data when created() but it creates the same duplication Commented Sep 19, 2020 at 21:09
  • 1
    Ok I will check it out. Thanks Commented Sep 19, 2020 at 22:26
  • 1
    @Daniel_Knights thanks for the input there was some funky circular logic happening in my if statements that created strange behaviors. I tried to give you an upvote on your comment so thanks. Commented Sep 20, 2020 at 6:21

1 Answer 1

1

To anyone having similar issues where your dev tools store does not match your actual store values output, it is probably due to your code not updating the store values formally through the actions and mutations approach.

if this store value is ever updated directly without actions and mutations the value in the store will change, however, those updated values will not be detected by vuejs dev tools and your actual store data and dev tools data values will not match.

Sign up to request clarification or add additional context in comments.

Comments

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.