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

99
Views
res.data.forEach is not a function in Jquery

I have got an error: Uncaught TypeError: res.data.forEach is not a function

when I use forEach with axios.get. How can I resolve problem?

script:

let totalData = [];
let monthData = [];

$(async function () {
  await axios.get(`/dashboard/chart/month`).then((res) => {
      console.log(res.data);

      res.data.forEach((data) => {
        totalData.push(data.total);
        monthData.push(data.months);
      });

    });

});

Data:

{
    "success": true,
    "data": [
        {
            "months": "January 21",
            "total": 0
        },
        {
            "months": "February 21",
            "total": 0
        },
        {
            "months": "March 21",
            "total": 0
        },
        {
            "months": "April 21",
            "total": 0
        }
    ],
    "msg": null
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ah, I just realised. Look at your res.data and you need res.data.data. Replace your code with:

let totalData = [];
let monthData = [];

$(async function () {
  await axios.get(`/dashboard/chart/month`).then((res) => {
      console.log(res.data);

      // Add another data here:
      res.data.data.forEach((data) => {
        totalData.push(data.total);
        monthData.push(data.months);
      });

    });

});

The res.data is the Axios's version. It has your data + status and other stuff. You have your data as well. So you need res.data.data.

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!