The problem is that if an ID is not in the database it will return a 404 error, I tried to make a check in if the res.status == 404 but it returns the error before going to the validation.
export const getServerSideProps = async (context) => {
const { id } = context.query;
const res = await axios.get(`${process.env.API_URL}/user/jobs/${id}`); //if ID does not exists here returns an error an does not validate the below lines
if (res.status == 404) { //not check if the above request returns a 404
return {
notFound: true,
};
}
const jobs = res.data;
return {
props: {
jobs,
},
};
};
CodePudding user response:
Rather than creating a conditional based on the result, you should create an error handler. How to handle reactjs errors: https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html
CodePudding user response:
Instead of axios, use fetch.
export const getServerSideProps = async (context) => {
const { id } = context.query;
const res = await fetch(`${process.env.API_URL}/user/application/${id}`);
const data = await res.json();
console.log(data);
if (data.success == false) {
return {
notFound: true,
};
}
const jobs = data.job;
return {
props: {
jobs,
},
};
};