TypeError when using generateStaticParams with Typescript enum
Answered
Harlequin posted this in #help-forum
HarlequinOP
I have a dynamic route in my NextJS application that I'm trying to set up static routes with based on a typescript enum that I'm importing. This is within a turborepo where the enum is being imported from another package in the monorepo. When I try to load the dynamic route I get the following error:
This is what the page in question looks like:
The console log seems to indicate that the enum is present when the generateStaticParams() function is called, so I'm not sure what the issue is.
TypeError: Cannot use 'in' operator to search for 'agenda' in undefined
This is what the page in question looks like:
import { agendas } from "@repo/seer-data";
import { AgendaName } from "@repo/seer-types";
import SidebarNav from "@/components/sidebar-nav";
export async function generateStaticParams() {
console.log(Object.values(AgendaName));
return Object.values(AgendaName).map((agenda) => {
agenda;
});
}
export default async function Agenda({
params,
}: {
params: Promise<{ agenda: AgendaName }>;
}) {
const agenda = (await params).agenda;
const agendaData = agendas[agenda];
return (
<SidebarNav
current={agenda}
label="Agendas"
links={Object.keys(agendas).map((agendaName) => ({
label: agendaName,
href: `/agendas/${agendaName}`,
}))}
>
<h1>{agenda}</h1>
</SidebarNav>
);
}
The console log seems to indicate that the enum is present when the generateStaticParams() function is called, so I'm not sure what the issue is.
Answered by gin
try actually mapping each value to the agenda key
return Object.values(AgendaName).map((agenda) => ({
agenda,
}));
10 Replies
HarlequinOP
Here is a screenshot of the error as well
HarlequinOP
Bump
and if so, you might need to check how you return your mapped AgendaName
if agenda is undefined your generateStaticParams() doesnt return correctly
try actually mapping each value to the agenda key
return Object.values(AgendaName).map((agenda) => ({
agenda,
}));
Answer
HarlequinOP
Yup that was my bad, didn't realize I was returning nothing there. Thank you
no problem! u can mark my answer as solution -)