When trying to set a cookie with cookie-universal-nuxt in my router middleware I get the error:
TypeError: Cannot read properties of undefined (reading 'set')
What is causing this?
nuxt.config.js
modules: [
'cookie-universal-nuxt',
],
router.js (router middleware)
export default async ({ app }) => {
app.$cookies.set('cookie-name', 'cookie-value', {
path: '/',
maxAge: 60 * 60 * 24 * 7
});
}
I tried logging in different places:
When I run console.log(app.$cookies) in the router it returns undefined.
When I run it in a component (so: console.log(this.app.$cookies)) it returns Cannot read properties of undefined (reading 'app').
I am using the composition-api so I also tried console.log(context.root.$cookies) but this also returns undefined.
console.log(app.$cookies)it returnsundefined.console.log(app.$cookies)? For example, within the script tags of a component you'd need to refer tothis.app.$cookies. Elsewhere in your app,thismay not refer to the root Vue instance on whichappand$cookiesetc should be available. Please provide more detail :)