question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getServerSideProps cannot be serialized as JSON. Please only return JSON serializable data types

See original GitHub issue

Bug report

Describe the bug

I’m returning a row set from MySQL (using the mysql2 library) in my getServerSideProps. Getting the following error:

SerializableError: Error serializing .data[0]returned fromgetServerSidePropsin "/reports/[report]/[start]/[end]". Reason:object ("[object Object]") cannot be serialized as JSON. Please only return JSON serializable data types.

But calling JSON.stringify(data) works just fine.

If I do a JSON.parse(JSON.stringify(data)) it works just fine, but that seems very odd.

To Reproduce

  1. Execute a query using the mysql2 library
  2. Return the row set as a prop in getServerSideProps
  3. Get Error.

Expected behavior

I expect the data to be serialized without an error since JSON.stringify() works just fine.

Additional context

Appears others are having this problem: https://stackoverflow.com/questions/61188494/getserversideprops-and-mysql-rowdatapacket

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:61
  • Comments:31 (5 by maintainers)

github_iconTop GitHub Comments

102reactions
timneutkenscommented, Apr 22, 2020

The main reasoning behind this limitation is that with getInitialProps we used to allow anything that can be stringified and that resulted in some really hard to track down UX bugs when hydrating client-side. Eg the hydration would fail because suddenly the Date object you had server-side is a string client-side.

We need to serialize the result of getStaticProps/getServerSideProps because it has to be sent to the browser to do hydration.

So we basically went with a strict checking mechanism (only runs in development) in order to prevent this from happening. We can potentially add an option to bypass in the future if there’s enough need for it 👍

100reactions
mayrsaschacommented, Apr 13, 2021

@timneutkens I don’t get why this issue is closed. It seems quite a lot of people are still stumbling upon this and there is demand for a better solution. A warning in development mode seems to be sufficient to help users track down hydration errors and it wouldn’t require everyone to use hacks to serialize dates and such.

Read more comments on GitHub >

github_iconTop Results From Across the Web

object` ("[object Response]") cannot be serialized as JSON?
getStaticProps and getServerSideProps only allow serializable content to be returned from them. To fix the issue you'll first need to ...
Read more >
How to fix error serializing Date object JSON in Next.js
This is because you must return a JSON-serializable object. A Date object cannot be natively transformed into JSON.
Read more >
I've spent the whole day on this, but no luck, I wanna die - Reddit
Reason: `object` ("[object Promise]") cannot be serialized as JSON. Please only return JSON serializable data types. Here is my function:
Read more >
Dealing with Date objects in Next data fetching - James Perkins
Data fetching with dates in Next.js usually lead to a error serializing due to date timestamps. ... Please only return JSON serializable data...
Read more >
error serializing `.csrftoken` returned from `getserversideprops`
Reason: object ("[object Object]") cannot be serialized as JSON. Please only return JSON serializable data types. But calling JSON.stringify(data) works ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found