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

209
Views
Getting error of fetched items after refreshing the page

I am fetching my data from external API as usual and this is the typical way I do it:

Fetch API:

 const [tshirts, setTshirts] = useState([]);

    const fetchData = () => {
        fetch('apiEndpoint')
            .then((response) => response.json())
            .then((data) => {
                setTshirts(data[0].clothes.regular.top); // path to my array
            })
            .catch((error) => {
                console.log(error);
            });
    };
    React.useEffect(() => {
        fetchData();
    }, []);

Map through an array:

const tshirtArray = tshirts.tShirt; // specifying the path
const listItems = tshirtArray.map((item) => <li>{item}</li>);

<ul>{listItems}</ul>

Example of data structure:

[
  {
  id: 1,
  clothes: {
     regular: {
      top: {
        sleeveless: [],
        tShirt: [
          "image-path-here"
          ],
.....
.....
.....

When I first time execute the code it works, but after some time or after refreshing the page I get an error of TypeError: Cannot read properties of undefined (reading 'map')

Why is that undefined? The path is correct and fetching the array should be as well. Can not find the reason of it not working.

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

0

I don't have reputation to comment, so let me try to clarify it for you through an answer. As @sojin mentioned, you cannot use tshirts.Tshirt since your state is of array type and arrays can't be used like objects, meaning that if there was an object of lets say exampleObject = { type: "shirt", color: "white } you could call it with exampleObject.type. Since you have an array of objects in your state (top that you are saving to state is still object which contains tShirt array), you first have to use index (to tell which object you want to use from the state array) and then you can use it like you wanted. For example, in your example there are 1 objects in state array. Array indexes start at 0. So you could do tshirts[0].tShirt to get the tShirt array from that object.

However, I would edit your code a bit. Instead of using tshirtArray constant, just do listItems from your state:

const listItems = tshirts.map((item) =>

  • {item.tShirt[0]}
  • );

    Note: I've just used index 0 here to demonstrate the finding of the first item in tShirt array. If you want to see all tShirt image paths, then you may need to do nested mapping or other similar solutions.

    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!