Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda