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

190
Views
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
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!