Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js Error serializing `.res` returned from `getServerSideProps`

I am getting the error below, when I use getServerSideProps function to retrieve data from Binance API.

import binance from "../config/binance-config";

export async function getServerSideProps() {

  const res = await binance.balance((error, balances) => {
    console.info("BTC balance: ", balances.BTC.available);
  });

  return {
    props: {
      res,
    },
  };
}

import Binance from "node-binance-api"

const binance = new Binance().options({
  APIKEY: 'xxx',
  APISECRET: 'xxx'
});

export default binance;

Error output:

Error: Error serializing `.res` returned from `getServerSideProps` in "/dashboard".
Reason: `undefined` cannot be serialized as JSON. Please use `null` or omit this value.

I'm not sure how to resolve this error. I would just like to be able to mine (and display) the response by sending it as props in another component.

Thank you!

like image 372
Fyl Avatar asked May 10 '26 18:05

Fyl


1 Answers

Here is how I solved it in NextJs

// Get Data from Database
export async function getServerSideProps(ctx) {
  const { params } = ctx;
  const { slug } = params;

  await dbConnect.connect();
  const member = await Member.findOne({ slug }).lean();
  await dbConnect.disconnect();

  return {
    props: {
      member: JSON.parse(JSON.stringify(member)), // <== here is a solution
    },
  };
}
like image 111
DiaMaBo Avatar answered May 12 '26 07:05

DiaMaBo