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

160
Views
TypeError: Cannot read properties of undefined (reading 'customer')
import React, { useState, useEffect } from 'react';
import axios from 'axios';

{const CustomerList = () => {
  const [information, setInformation] = useState([]);
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    if (isLoading) {
      axios.get('').then((response) => {
        setInformation((prevInfo) => {
          return [...prevInfo, response.data];
        });
      });
    }
    return () => {
      setIsLoading(false);
    };
  }, [information, isLoading]);

  return (
    <>
      <h2>Name: </h2>
      {information.map((item, index) => (
        <div>
          <div>{item[index].customer.firstName}</div>
          <div>{item[index].customer.lastName}</div>
        </div>
      ))}
    </>
  );
};

export default CustomerList;
  1. I am fetching data from an api for work using axios and useeffect
  2. I map through the response array of data and if no info is in the arraythe app breaks
  3. How can I map through only the specific amount of items in an array?
  4. For example if I have 3 items in my array of data, it will show them on the browser down from the return statement BUT... when it maps all 3 it will continue to try again and break hence 'customer is undefined' because there is no more customers 5.I have tried to do {item.customer.firstName and get an error of undefined first name}

Any way to get around this? Thanks!

enter image description here

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

0

That's not how map works. With these arguments to the callback:

information.map((item, index) => ...

index is the current index when iterating over the information array, and item is the element at that index. It's not another copy of the entire array. So instead of this:

item[index].customer.firstName

You would just do this:

item.customer.firstName

As an aside, when iterating over an array to output a list in React you would want to add a key to the top-level element. You can use index for this purpose if you like:

{information.map((item, index) => (
    <div key={index}>
      ...
    </div>
))}

Or if item has a unique identifier then you can use that instead and remove index entirely.

about 4 years ago · Juan Pablo Isaza Report

0

item[index] is not correct, since item refers to the element of the array at that index anyway.

Correct Way:

const data = [1,2,3,4,5,6,7,8,9,10]

const mappedData = data.map((d, index) => {
    console.log("index is", index)
    console.log("d is", d)
    console.log("-----------------------------")
    return d
})

What you are trying to do:

const data = [1,2,3,4,5,6,7,8,9,10]

const mappedData = data.map((d, index) => {
  console.log("index is", index)
  console.log("d[index] is", d[index]) //undefined since d is not an array 
  console.log("------------------------------")
  return d
})

about 4 years ago · Juan Pablo Isaza Report

0

When you map() an array of items, you are handling each item separately, therefore you don't need the index, you could use the index as key, read more about lists and keys here.

{information.map((item, i) => (
  <div key={i}>
    <div>{item.customer.firstName}</div>
    <div>{item.customer.lastName}</div>
  </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!