I am encountering a problem with my Next.js Application that utilizes a [slug] for Static Site Generation. Everything works perfectly fine on my localhost, but when I try to deploy it, I encounter the following error:
“Unhandled error during request: TypeError: Cannot read property ‘title’ of undefined”.
Additionally, when running the next build command on my localhost, I get this error:
Error occurred prerendering page "/jobs/[slug]". Read more: https://nextjs.org/docs/messages/prerender-error TypeError: Cannot read property 'title' of undefined
Below is the code snippet for reference:
export default function Listing({ job }) {
const router = useRouter()
if (!router.isFallback && !job?.slug) {
return <ErrorPage statusCode={404} />
}
return (
<div >
<div >
<div >
<div >
<div >
<div>
<h1>
<span>Job Center</span>
<span >{job.title}</span>
<p>We are looking for interested candidates for the following position. </p>
</h1>
<div>
<div >
<span>Position: </span><span>{job.title}</span> //and multiple fields like this
</div>
</div>
</div>
</div>
</div>
</div>
)
}
export async function getStaticProps({ params, preview = false }) {
const data = await getJobAndMoreJobs(params.slug, preview)
return {
props: {
preview,
job: data.job
},
}
}
export async function getStaticPaths() {
const jobs = await getAllJobsWithSlug()
return {
paths: jobs.map(({ slug }) => ({
params: { slug },
})),
fallback: true,
}
}
In addition, there is an API file that fetches data from a GraphQL Schema and Query. The following code snippet shows how the API file functions:
async function fetchAPI(query, { variables, preview } = {}) {
const res = await fetch(process.env.JOBS_PROJECT_API, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${
preview
? process.env.JOBS_DEV_AUTH_TOKEN
: process.env.JOBS_PROD_AUTH_TOKEN
}`,
},
body: JSON.stringify({
query,
variables,
}),
})
const json = await res.json()
if (json.errors) {
console.log(process.env.NEXT_EXAMPLE_CMS_GCMS_PROJECT_ID)
console.error(json.errors)
throw new Error('Failed to fetch API')
}
return json.data
}
export async function getPreviewPostBySlug(slug) {
const data = await fetchAPI(
`
query PostBySlug($slug: String!, $stage: Stage!) {
post(where: {slug: $slug}, stage: $stage) {
slug
}
}`,
{
preview: true,
variables: {
stage: 'DRAFT',
slug,
},
}
)
return data.job
}
export async function getJobAndMoreJobs(slug, preview) {
const data = await fetchAPI(
`
query JobBySlug($slug: String!, $stage: Stage!) {
job(stage: $stage, where: {slug: $slug}) {
title
section
slug
vacancies
rank
classification
placeOfWork
basicSalary
serviceAllowance
allowances {
name
percent
requirement
}
responsibilities
requirement
documents
expirationDate
expectedInterviewDate
gazetteLink
a2Form {
url
}
}
moreJobs: jobs(orderBy: publishedAt_DESC, first: 2, where: {slug_not_in: [$slug]}) {
title
slug
title
section
slug
vacancies
rank
classification
placeOfWork
basicSalary
serviceAllowance
expirationDate
expectedInterviewDate
}
}
`,
{
preview,
variables: {
stage: preview ? 'DRAFT' : 'PUBLISHED',
slug,
},
}
)
return data
}
If anyone can provide assistance or insights into resolving this issue, it would be greatly appreciated! Thank you!