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

189
Vistas
Handling errors in java script

I have problem in JavaScript when it comes handling errors. This is the code:

    const [file, setFile] = useState(null);
  const {user} = useContext(Context);

  const handleSubmit = async (e, resp) =>{
    e.preventDefault();
    const newPost = {
      username:user.username,
      title,
      desc,
    };
    if(file){
      const data = new FormData();
      const filename = Date.now() + file.name;
      data.append("name", filename);
      data.append("file",file);
      newPost.photo = filename;
      try{
        await axios.post("/upload",data);
      }catch(err){
        console.log(err);
      }
    }
    try{
    const res = await axios.post("/posts", newPost);
    window.location.replace("/post/"+res.data._id);
    }catch(err){
      console.log(err);
    }
  };

I am pretty sure that mistake comes from handling errors because I am getting this error: POST http://localhost:3000/posts 500 (Internal Server Error) When I handled errors correctly I didn't have this. But now not sure what to write inside catch block so I can get rid of this error.

about 4 years ago · Juan Pablo Isaza
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