js-cookie: Cookies.get() always returns undefined

When I am trying to get cookies values in my NUXT.js project I always receive undefined

import Cookies from 'js-cookie'

// ...

let token = Cookies.get('token')
console.log(token) // undefined

let cookies = Cookies.get()
console.log(cookies) // undefined

So I use this approach now:

const tokenCookie = request.headers.cookie
  .split(";")
  .find(c => c.trim().startsWith("token="));
if (!tokenCookie) {
  return
}
token = tokenCookie.split('=')[1]

but it’s not so elegant as I want.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

I just tried Cookies.getItem('token') and received this TypeError

js_cookie__WEBPACK_IMPORTED_MODULE_0___default.a.getItem is not a function

I just installed js-cookies on my WebPack/Vue project and noticed that the method names are different from what is listed in the readme. Cookies.get( 'foo' ) generates an error, where Cookies.getItem('foo') seems to be the new nomenclature.

EDIT: My mistake. I npm installed a different package — js-cookies, not js-cookie.

Looks like I have no document object on that stage

That‘s what I suspected…