Search code examples
vuexnuxt.jsserver-side-rendering

req is not defined - nuxtServerInit


I'm trying to set up Firebase Auth with Vuex and Nuxt using this tutorial. However if I console.log(req) it's undefined

In the tutorial the vuex action is:

nuxtServerInit ({ commit }, { req }) {
  if (req.session && req.session.authUser) {
     commit(‘SET_USER’, req.session.authUser)
     req.session.cookie=”” + req.session.authUser
  }
 }

But no where in the tutorial was it actually defined. Even straight from the docs they have:

actions: {
  nuxtServerInit ({ commit }, { req }) {
    if (req.session.user) {
      commit('user', req.session.user)
    }
  }
}

But I can't find where you initially set req or how to access it in the Vuex store


Solution

  • You need to create a store/index.js file and define the nuxtServerInit action in there as you have in your second example.

    https://nuxtjs.org/docs/2.x/directory-structure/store/#the-nuxtserverinit-action