ydrea
ydrea

Reputation: 95

Error: Error serializing props returned from getServerSideProps in "..."

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?

Upvotes: 1

Views: 975

Answers (1)

Youssouf Oumar
Youssouf Oumar

Reputation: 46221

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

Upvotes: 2

Related Questions