Next.js Discord

Discord Forum

Cookie undefined (return "{}")

Answered
American black bear posted this in #help-forum
Open in Discord
Avatar
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.
Answered by Z4NR34L
when you change this cookie to be not only httpcookie, you should be able to read it on client-side
View full answer

43 Replies

Avatar
American black bearOP
Axios
Image
No refresh page
Image
with refresh page:
Image
Avatar
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
Avatar
American black bearOP
The page.tsx is client..
Avatar
so you can use for example:

allCookies = document.cookie;
and just read them from document on client-side by your own
Avatar
American black bearOP
That what i did but i get error "document is not define" something like this
Avatar
yeah, give me a minute so I would find suitable resources as I’m on phone currently 🫡
Avatar
American black bearOP
That fine take time all good :blob_wave:
Avatar
btw, can you share page code here? or repository url if it’s public
Avatar
American black bearOP
Sure
Image
Image
Avatar
simpliest solution would be calling document.cookie inside useEffect, as it’s runned after component mount (when document is already defined)
Avatar
American black bearOP
And here
Image
Oh okay let me check wait
Avatar
sure, take ur time
Avatar
American black bearOP
Like this right?
Image
if yes that showing on console browner this
Avatar
yeah
Avatar
American black bearOP
Image
like nothing something
Image
Avatar
change this useeffect to console.log(document.cookie)
Avatar
American black bearOP
I did same like this rn and that saying same
Image
Avatar
do I see right that this cookie is httponly? :>
Avatar
American black bearOP
Yes
That shouldn't be httponly?
Avatar
httponly cookies cannot be accessed via clientside 😄
Avatar
American black bearOP
Ohhhh that why the console was saying "next js server"
.
Avatar
hell yeah
Avatar
when you change this cookie to be not only httpcookie, you should be able to read it on client-side
Answer
Avatar
American black bearOP
run to change :blob_wave:
Avatar
🤞
Avatar
American black bearOP
TYY
I was sit alot time to find way to fix this :lolsob:
Avatar
Happy that helped!
We are here to help 😉 If you will run into further issues feel free to post here!
Avatar
American black bearOP
Thank you ❤️