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

445
Views
Getting "Uncaught TypeError: Cannot read properties of undefined (reading 'map')" when using Chartjs inside react axios method

I was trying to create a chart using Chartjs in react. For that I used axios post method to get data from database from server that I created in Nodejs. When I used chartjs in axios I am getting this error "Uncaught TypeError: Cannot read properties of undefined (reading 'map')"

Here is my code:

const [userData, setData] = React.useState([]);
React.useEffect(()=> {
    axios.post("http://localhost:3001/graph",
     {
        username:str
     }).then(response=>{

         Uname = response.data.username;
         exp = response.data.expense;
         inc = response.data.income;
         console.log(inc);
         console.log(exp);
      
        data.push(exp);
        data.push(inc);
         
     });
    setData({    
        labels: title,
        datasets:[
        {
            label: "Expense",
            data: data,
            backgroundColor: ["red"]
        }
        ]
    });
    
    console.log(userData);
})

I am getting empty array in userData. I tried most of the method online but nothing seems to work. Please if anyone can help me here its really imp.

Thank you.

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

0

Its because at first the userData is an empty array and after a while data goes to it and you can use it; one think that you can do is to use an if before defining map like this: {userData && userData.map ...}

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!