Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

324
Visualizações
Navigating to dynamically routed page in Next JS returns error

When navigating from a link in the same web app to the dynamically routed page clicking on a link the result is as intended: I navigate to the page for a product (http://localhost/1).

But when I directly navigate by naming the product number specifically in the search bar (navigating to http://localhost/2), I get the following error:

Server Error
TypeError: Cannot read property 'image' of undefined 

> | <Image src={"/../public/images/" + p.image}
                                        ^

So far I've tried making the types match and reading the Next JS docs on dynamically routing.

I've removed the array zero from the filter but still no resolution.

Could it be possible that the routing only works when clicking on a link in Next JS? Is there some missing setting I've neglected?

pages/[pid].js

import { useRouter } from 'next/router'
import Image from 'next/image'

import data from '../products.json'

export default function Template() {
  const router = useRouter()
  const { pid } = router.query

  const p = data.filter(product => product._id == pid)[0]  // Choose one result

  return (
    <Image src={"/../public/images/" + p.image}
           height="500px"
           width="500px" />
  )
}

products.json

[
  {
    "_id": 1,
    "name": "Toyota",
    "image": "toyota.png"
  },
  {
    "_id": 2,
    "name": "BMW",
    "image": "bmw.png"
  }
]

Update: I've tried to hardcode the src attribute in the Image tag and the new error says the other references are the issue. So I can safely say the issue is to do with no object returned when the data object is called.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I solved the issue!

It was not enough to use Dynamic Routes by using the 'useRouter()' function. I also had to define these two functions:

export async function getStaticProps(context) {
  // No-op since getStaticPaths needs getStaticProps to be called.
  return { props: {} }
}

export async function getStaticPaths() {
  const dynamicFiles = products.map(product => (
    {
      params: { pid: String(product._id) },  // Product IDs are ints in JSON file
    }
  ))

  return {
    paths: dynamicFiles,
    fallback: false
  }
}

This makes sense since you wouldn't want random paths to be used as a variable. For example, then a user would be able to specify http://localhost/1234 when 1234 is not a valid option.

https://nextjs.org/learn/basics/dynamic-routes/implement-getstaticprops

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda