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

220
Views
React / Axios: Cannot read properties of undefined (reading '0')

I can see array with objects of data "fees" perfectly but if i wanna see a single value like fees[0]?.firstFee i get this error:

Uncaught TypeError: Cannot read properties of undefined (reading '0')

i thought ? operator would avoid this but seems like not...

const [fees, setFees] = useState();

useEffect(() => {
    const getFees = async () => {
      const res = await axios.get('/api/feesettings', {
        headers: { Authorization: token },
      });
      setFees(res.data);
    };

    getFees();
  }, []);

  console.log(fees);
  console.log(fees[0]?.firstFee );
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

The reason why you are getting an error on every render is because, you are passing no value to the state, so by default fees is undefined

const [fees, setFees] = useState();

and when you reach this line of code, you are trying to access fees[0] as if fees is an array, it is not that's why it's giving you an error Cannot read properties of undefined (reading '0')

console.log(fees[0]?.firstFee);

So change to this line of code,

const [fees, setFees] = useState([]);

if (fees.length > 1) {
    console.log(fees[0]?.firstFee);
}
about 4 years ago · Santiago Gelvez 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!