nextjs-corporate-starter icon indicating copy to clipboard operation
nextjs-corporate-starter copied to clipboard

[ PRs Welcome ] Error when fetching page data

Open Tumpelox opened this issue 1 year ago • 4 comments

I have followed all needed steps but I get this error.

wait  - compiling...
event - compiled client and server successfully in 375 ms (265 modules)
wait  - compiling /src/middleware (client and server)...
event - compiled successfully in 73 ms (73 modules)
wait  - compiling /[lang]/page (client and server)...
event - compiled client and server successfully in 1303 ms (629 modules)
error - SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
error - SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
error - SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
error - SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
error - SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
error - src\app\[lang]\page.tsx (16:41) @ attributes
error - TypeError: Cannot read properties of undefined (reading 'attributes')
    at RootRoute (./src/app/[lang]/page.tsx:28:42)
  14 | export default async function RootRoute({ params }: { params: { lang: string } }) {
  15 |     const page = await getPageBySlug('home', params.lang);
> 16 |     const contentSections = page.data[0].attributes.contentSections;
     |                                         ^
  17 |
  18 |     return contentSections.map((section: any, index: number) => sectionRenderer(section, index));
  19 | }

If I console log the page variable I get this:

{
  data: [],
  meta: { pagination: { page: 1, pageSize: 25, pageCount: 0, total: 0 } }
}

I noticed that params.lang will get value 'favicon.ico' but when request made with getPageBySlug('home', 'en'); I get correct data

{
  data: [ { id: 1, attributes: [Object] } ],
  meta: { pagination: { page: 1, pageSize: 25, pageCount: 1, total: 1 } }
}

But still there is this and nothing shows up at the browser

wait  - compiling /[lang]/page (client and server)...
event - compiled successfully in 465 ms (276 modules)
error - SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
error - SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
error - SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)

Data seeding didn't work so I filled it manually (page with the home slug and global)

Windows 11

Tumpelox avatar Apr 21 '23 00:04 Tumpelox