Getting Uncaught (in promise) ReferenceError: getDefaultState is not defined
when trying to use a store reset function on a module store. Nuxt documentation on store setup unfortunately is not very thoughrough. Can anyone point me to what I'm missing to make this available on the root level?
My store reset is based on:
Vuex issue 118
The above is used here with a state function
My store setup is based on:
Latest implementation, that I actually used
Along with this very well structured answer by CMarzin
Framework documentations used:
Vuex documentation
and
Nuxt store documentation
Not related:
This is about general store usage
My setup:
└── store
└── user
└── state.js
└── getters.js
└── mutations.js
└── actions.js
└── state.js (root state)
└── getters.js (root getters)
└── mutations.js (root mutations)
└── actions.js (root actions)
user/state.js
:
const getDefaultState = () => {
return {
counter: 2,
user: false,
}
}
const state = getDefaultState()
export default {
state,
}
user/mutations.js
:
export default {
ON_AUTH_STATE_CHANGED_MUTATION: (state, { authUser, claims }) => {
if (authUser) {
const { uid, email, emailVerified, displayName, photoURL } = authUser
state.user = { uid, email, emailVerified, displayName, photoURL }
} else {
state.user = false
}
},
RESET_USER_STATE(state) {
// Merge rather than replace so we don't lose observers
// https://github.com/vuejs/vuex/issues/1118
// https://stackoverflow.com/questions/42295340/how-to-clear-state-in-vuex-store
console.log(state)
Object.assign(state, getDefaultState())
},
}
user/actions.js
:
export default {
async onAuthStateChanged({ commit, dispatch }, { authUser }) {
if (!authUser) {
dispatch('resetUserState')
return
}
if (authUser && authUser.getIdToken) {
try {
const idToken = await authUser.getIdToken(true)
console.info('idToken', idToken)
} catch (e) {
console.error(e)
}
}
commit('ON_AUTH_STATE_CHANGED_MUTATION', { authUser })
},
resetUserState({ commit }) {
commit('RESET_USER_STATE')
},
}