blitz-js / babel-plugin-superjson-next

Automatically transform your Next.js Pages to use SuperJSON
MIT License
125 stars 15 forks source link

Serializable error #12

Closed blakeley closed 3 years ago

blakeley commented 3 years ago

I'm getting the following error on one of my pages:

SerializableError: Error serializing `.meta` returned from `getServerSideProps` in "/account-settings".
Reason: `undefined` cannot be serialized as JSON. Please use `null` or omit this value all together.

Here's my code:

export const getServerSideProps: GetServerSideProps<IProps | {}> = async (
  context
) => {
  const token = parseCookies(context)["token"];
  try {
    const userId = Number(jwt.verify(token, process.env.APP_SECRET!));
    const user = await prisma.user.findOne({
      where: { id: userId },
    });
    if (!user) throw new Error("Could not find user");
    return { props: { user } };
  } catch {
    context.res.statusCode = 302;
    context.res.setHeader("Location", `/`);
    return { props: {} };
  }
};

I've tried multiple variations of setting user to null, but none seem to fix the issue. My guess is that babel-plugin-superjson-next is leaving its .meta field as undefined (a rare case, but something that makes sense for a redirect, for instance), and Next.js throws an error on that case.

Changing the last line to the following, however, avoids this issue:

    return { props: {date: new Date()} };
Skn0tt commented 3 years ago

This should have been solved with https://github.com/blitz-js/babel-plugin-superjson-next/pull/6. Are you on the latest version?

blakeley commented 3 years ago

Yes, upgrading to 0.1.7 fixed it! Thanks!