I am using vue-cookie
package which lets me to set and get cookies with ease. What I want is to get this cookie in nuxtServerInit()
:
async nuxtServerInit() {
const res = await this.$axios.post('/me', {}, {
headers: {
'Authorization': 'Bearer ' + $nuxt.$cookie.get('token')
}
})
}
But, I always get $nuxt is not defined
error. Please help!
I am using vue-cookie
package which lets me to set and get cookies with ease. What I want is to get this cookie in nuxtServerInit()
:
async nuxtServerInit() {
const res = await this.$axios.post('/me', {}, {
headers: {
'Authorization': 'Bearer ' + $nuxt.$cookie.get('token')
}
})
}
But, I always get $nuxt is not defined
error. Please help!
1 Answer
Reset to default 9vue cookie is a wrapper around tiny-cookie . Tiny cookie is for browser. So it wont work on server e.g. in nuxtServerInit
In nuxtServerInit you should get cookies from req.cookies
async nuxtServerInit(_, { req }) {
console.log(req.headers.cookie)
}