Next.js Discord

Discord Forum

Cookie Parsing error

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
Hi,

I am js-Cookie to set the Cookie in my browser. In my nextjs project.
When I am accessing cookies on the client side and Parsing it's giving me inconsistent results.
When I am accessing, cookies in my nextjs middleware it gives me consistent results without any errors.
I understand that it might be parsing error because the cookie is saved in the string format.

1. Want to know how can I resolve it and what's a better way to save object in a cookie .
2. What to do in case there is a special character in the object that is stored in cookie.
Cause JSON.stringify will convert the cookie but Parsing will throw an error.

4 Replies

Asian black bear
one kind of "I give up" option would be to store btoa(JSON.stringify(...stuff)
then to "refresh" it with JSON.parse(atob(cookieTextBlob))
Cape lionOP
Thanks, I will try this out
Cape lionOP
I tried this out similar to the previous thing I mentioned it's working sometimes and sometimes it's not
I am trying to save this object in the cookie
user: {
name: 'Ash',
subscribed: false,
lastUploaded: '2023-09-15T10:13:52.370Z'
subscriptionDetails:{}
}