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

157
Visualizações
Uncaught TypeError: workouts.map is not a function "react"

this is the JSON file that is coming from the server

 {workouts: Array(3)}
workouts: Array(3)
0: {_id: 'idnumber1', title: 'pullup', reps: 40, load: '20', createdAt: '2022-07-20T18:06:39.642Z', …}
1: {_id: 'idnumber2', title: 'situp', reps: 40, load: '20', createdAt: '2022-07-20T10:10:14.078Z', …}
2: {_id: 'idnumber3', title: 'pushup', reps: 10, load: '0', createdAt: '2022-07-19T14:33:58.678Z', …}
length: 3
[[Prototype]]: Array(0)
[[Prototype]]: Object

//this is the code I did in react

import { useEffect, useState } from "react";

const Home = () => {
  const [workouts, setworkouts] = useState(null);

  useEffect(() => {
    const fecthworkout = async () => {
      const response = await fetch("/api/workout");
      const json = await response.json();

      if (response.ok) {
        setworkouts(json);
      }
    };

    fecthworkout();
  }, []);

  useEffect(() => {
    console.log("hello");
  }, []);

  return (
    <div className="home">
      <div className="workouts">
 
        {workouts &&
          workouts.map((workouts) => (
           <p key={workouts._id}>{workouts.title}</p>
          ))}
      </div>
    </div>
  );
};

export default Home;

//can any tell me how can I stop the error?

I am stuck here I need help here

I am stuck here I need help here

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your usestate hook should by default be useState( [ ] ) and not useState(null)

about 4 years ago · Santiago Trujillo 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