Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

98
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda