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

278
Views
find(...) is undefined when fetching data with dynamic url using next.js

When I'm fetching data in my main page everything works as i wanted but when I'm fetching data in another folder using same code but with dynamic url, I got an error when I'm trying to using methods on array. When i console.log fetched data, I got the same array as in my main page

When i delete Link and only want to see book.title it works. But i got error when i want to get data from resources.

mainpage.js
const [data, setData] = useState(null);
const [isLoading, setLoading] = useState(false);

useEffect(() => {
  setLoading(true);
  fetch('https://gnikdroy.pythonanywhere.com/api/book')
    .then((res) => res.json())
    .then((data) => {
      setData(data);
      setLoading(false);
    });
}, []);

return(
       <div>
        {data.results.map((book, index) => (
          <div key={index}>
            <h1>{book.title}</h1>
            <Link href={`/reader/${book.id}`} passHref>
              <h2>
                {
                  book.resources.find(
                    ({ type }) => type === 'application/epub+zip'
                  ).uri
                }
              </h2>
            </Link>
          </div>
        ))}
      </div>
)
searchPage.js
const router = useRouter();
const { name } = router.query;
const [data, setData] = useState(null);
const [loading, setLoading] = useState(false);

useEffect(() => {
  setLoading(true);
  fetch(`https://gnikdroy.pythonanywhere.com/api/book/?search=${name}`)
    .then((res) => res.json())
    .then((data) => {
      setData(data);
      setLoading(false);
      console.log(data);
    });
}, []);

return(
      <div>
        {data.results.map((book, index) => (
          <div key={index}>
            <h1>{book.title}</h1>
            <Link href={`/reader/${book.id}`} passHref>
              <h2>
                {
                  book.resources.find(
                    ({ type }) => type === 'application/epub+zip'
                  ).uri
                }
              </h2>
            </Link>
          </div>
        ))}
      </div>
)

my error look like this

my console.log inside fetch in searchPage.js

console.log

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your response data is not getting resources fields sometime.
That's why book.resources can be undefined (or) null.

You can easily use Optional Changing(?.)

Replace:

{
   book.resources?.find(
     ({ type }) => type === 'application/epub+zip'
   )?.uri || ''
}
about 4 years ago · Juan Pablo Isaza Report

0

In addition to the Wu Woo's answer, When consider How React render your code,

First will execute the return and then useEffect only in page load since you're not having values in useEffect Dependency array.

So in your scenario, you have data = null in initial page load. So when render below code, since data = null data.results can't use Arrays.map().

Then will execute the useEffect and inside there you set the value return from API call and set that value to data.

To avoid that render undefined error you must ensure data.results are not equal to null/undefined and when there is data.results render what you want.

You can achieve that by below ways.

  1. The optional chaining operator (?.) as Wu Woo's answer which simplify accessing values through connected objects when it's possible that a reference or function may be undefined or null. Then it will not give above error when book.resources equal null/undefined.

  2. render only when there is value for data.results and book.resources otherwise not like below.

      // Here conditionally check whether there is value for data?.results and if so render this.
     {data?.results && data.results.map((book, index) => (
     <div key={index}>
       <h1>{book.title}</h1>
       <Link href={`/reader/${book.id}`} passHref>
         <h2>
           {/* Here also conditionally check whether there is value for book?.resources and if so render this. */}
           { book?.resources &&
             book.resources.find(
               ({ type }) => type === 'application/epub+zip'
             ).uri
           }
         </h2>
       </Link>
     </div>
     ))}
    
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!