Home

Issue: 193

nuxtServerInit just modify state in the same file

[issue link]

I was trying to create a module “auth” separate from the main module “index.js”, but when I try to modify the state of the module “auth” through the action nuxtServerInit simply does not work.

My index.js file

export default {
  actions: {
    nuxtServerInit({commit}, {req}) {
      if (req.auth) {
        commit('auth/SET_USER', req.auth)
      }
    }
  },
}

My auth.js file

export default {
  state: {
    user: null
  },
  mutations: {
    SET_USER({user}, payload) {
      user = payload
    }
  }
}

When I try to modify a state within the index.js file it works normally

example

export default {
  state: {
    ping: null
  },
  mutations: {
    SET_PING(state, payload) {
      state.ping = payload
    }
  },
  actions: {
    nuxtServerInit({commit}, {req}) {
      commit('SET_PING', 'PONG')
    }
  },
}
This question is available on Nuxt.js community (#c164)