profile
viewpoint

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

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

zeit/next.js

Answer questions chrisbrantley

+1 for an option to bypass.

I've disabled all type casting in mysql2 but the objects returned are still of type TextRow even though they are just plain javascript objects consisting of nothing but primitives. I have to do rows.map(row => ({...row})) to keep from triggering that error.

Given that the primary use case of this feature is to fetch data from some data store and shove it into props, I think this will become quite a pain.

Even if it was just a warning and not a hard error it could still be helpful in development.

useful!
source:https://uonfu.com/
Github User Rank List