Next.js Discord

Discord Forum

when i build and this got error how to solve this?

Answered
Turkish Angora posted this in #help-forum
Open in Discord
Original message was deleted.
Answered by Cape horse mackerel
View full answer

21 Replies

Can you show the code where you re using cookies()? Also which next.js ver are u using?
import { EnteredChatList, RecommendedChatList } from '@/types/types';
import { cookies } from 'next/headers';

const options = {
  credentials: 'include',
  headers: { Cookie: `ACCESS=${cookies().get('ACCESS')?.value}` },
} as const;

export async function fetchEnteredChatList(): Promise<EnteredChatList[]> {
  const url = `${process.env.SERVER_API_URL}/api/chatroom/participated`;
  const response = await fetch(url, options);
  return response.json();
}

@awareness481 i m using 13.5.2 version
Can you try upgrading to v14? (Or v13.5.6)
Turkish Angora
yes sir
@awareness481 same got error
Cape horse mackerel
can you please move options within fetchEnteredChatList block?
and try 🙂
I believe you can't access it from the global scope, it should be done inside a component or a route
Turkish Angora
wow it works
but i have 1 question
Do I have to put all options in the function repeatedly?
@Cape horse mackerel
If there are several functions like this..
fetchEnteredChatList1() ~ fetchEnteredChatList100()...
Cape horse mackerel
I mean, you can reuse that piece of logic, but as I said cookies() must be called within component or route in order to work properly.
Maybe try something like
const options = () => ({
credentials: 'include',
  headers: { Cookie: `ACCESS=${cookies().get('ACCESS')?.value}` }
} as const);

function yourFunction() {
  options();
}


I'm not sure right now, but yeah, you should be able to reuse that logic
Cape horse mackerel
just mark post as resolved/answered
Turkish Angora
ok
Cape horse mackerel
right click => apps => mark as resolved
🙂
Turkish Angora
sry i m new...
finding mark button rn...
Cape horse mackerel
Answer