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.