Cookie undefined (return "{}")
Answered
American black bear posted this in #help-forum
American black bearOP
Hello, I am trying to return to client about his cookies for get data/something like this.
So i tried alot ways to get cookies and i used package "next-cookies" (return {} and undefined) and next-client-cookies (Working on refresh page and not when after refresh page) and now i tried with Axios and that not working too.
So i tried alot ways to get cookies and i used package "next-cookies" (return {} and undefined) and next-client-cookies (Working on refresh page and not when after refresh page) and now i tried with Axios and that not working too.
Answered by Z4NR34L
when you change this cookie to be not only httpcookie, you should be able to read it on client-side
43 Replies
American black bearOP
Axios
No refresh page
with refresh page:
if you are using next 13+ try with:
import { cookies } from "next/headers"
console.log(cookies().get("Username"))
current versions of next, have build-in methods for headers support 🙂 If you will run into any problems feel free to ping me and I will assist you in further debugging
American black bearOP
The page.tsx is client..
so you can use for example:
allCookies = document.cookie;
allCookies = document.cookie;
and just read them from document on client-side by your own
American black bearOP
That what i did but i get error "document is not define" something like this
yeah, give me a minute so I would find suitable resources as I’m on phone currently 🫡
American black bearOP
That fine take time all good
btw, can you share page code here? or repository url if it’s public
American black bearOP
Sure
simpliest solution would be calling document.cookie inside useEffect, as it’s runned after component mount (when document is already defined)
American black bearOP
And here
Oh okay let me check wait
sure, take ur time
American black bearOP
Like this right?
if yes that showing on console browner this
yeah
American black bearOP
like nothing something
change this useeffect to console.log(document.cookie)
American black bearOP
I did same like this rn and that saying same
do I see right that this cookie is httponly? :>
American black bearOP
Yes
That shouldn't be httponly?
httponly cookies cannot be accessed via clientside 😄
American black bearOP
Ohhhh that why the console was saying "next js server"
.
hell yeah
when you change this cookie to be not only httpcookie, you should be able to read it on client-side
Answer
American black bearOP
run to change
🤞
American black bearOP
TYY
I was sit alot time to find way to fix this
Happy that helped!
We are here to help 😉 If you will run into further issues feel free to post here!
American black bearOP
Thank you â¤ï¸