简体   繁体   中英

Cookies.set doesn't set the value in cookie (js-cookie)

I'm trying to set the some data inside of cookie using js-cookie. but somehow it's not setting in the cookie. following is my code:

const setCookie = (name: string, value: string, expires: Date) => {
  Cookies.set(name, value, {expires})
}

and this is the result when I console.log this line:

 UserId=1; path=/; expires=Thu, 09 Apr 2020 15:26:37 GMT

I thought that path=/ is the problem, so I've tried to set the path as well, but didn't work. Does anyone have idea why is not setting into the cookie?

Updated: data passed to name, value, expires are:

UserId, 1, Thu Apr 09 2020 11:26:37 GMT-0400 (Eastern Daylight Time)

Expires parameter should be a number which indicate how much days until your cookie will expire

just update your function as follow

const setCookie = (name: string, value: string, expires: number) => {
  Cookies.set(name, value, {expires})
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM