Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

323
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!