0

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,
    },
  };
};

Image

n9p4
  • 304
  • 8
  • 34

2 Answers2

1

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

Cheese
  • 118
  • 10
  • thank you for your comment, ErrorBoundary would work if this would happen in client side inside component, the error occurs in the function ( getServerSideProps ) which is executed before the component is displayed – n9p4 Mar 22 '22 at 13:00
-1

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,
    },
  };
};
n9p4
  • 304
  • 8
  • 34