Home > Blockchain >  Error: Error serializing props returned from getServerSideProps in "..."
Error: Error serializing props returned from getServerSideProps in "..."

Time:12-15

Trying to insert a condition inside getServerSideProps:

export async function getServerSideProps(context) {
  const jwt = parseCookies(context).jwt || null;
  if (jwt) {
    const user = parseJwt(jwt);
    const userId = user.id;
    console.log(userId);
  } else {
    return {};
  }
  return {
    props: { jwt, userId },
  };
}

Getting the error:

Error: Error serializing props returned from getServerSideProps in "/account". Reason: Props must be returned as a plain object from getServerSideProps: { props: { ... } }.

How would you do it?

CodePudding user response:

getServerSideProps should return a really specific object, in the form of { props: {} }, which the code you showed not respecting. Try changing it to:

export async function getServerSideProps(context) {
  const jwt = parseCookies(context).jwt || null;
  if (jwt) {
    const user = parseJwt(jwt);
    const userId = user.id;
    return {
      props: { jwt, userId },
    };
  } else {
    return {
      props: {},
    };
  }
}
  • Related