How to get Json serializable data from Mongo Atlas

Viewed 15

The data that I am getting is that and this gives me an error when I am trying to pass through props and map over them. note:- data is in an array This is just a part of it. The error that I am getting

Reason: `object` ("[object Object]") cannot be serialized as JSON. Please only return JSON serializable data types.


 {
    _id: new ObjectId("631a3a40164bcbf69bb4745a"),
    title: 'yeets #351',
    price: 0.0025,
    image: 'yeets351.png',
    lastsale: 0.024,
    isFeatured: true
  },

and I need data in this form.

{
      _id: '631a3d5e164bcbf69bb47464',
      title: 'yeets #360',
      price: 0.0058,
      image: 'yeets360.png',
      lastsale: 0.001,
      isFeatured: true
    },

code for getting the data

export async function getStaticProps(context) {
const client = await connectionDb();
    const db = client.db('nfts');
    const data = await db.collection("nft-collection").find().toArray();

Now I can fetch my own api, but the problem is that we can do that only in production, and while making a build we need a direct connection to the database in getStaticProps.

0 Answers
Related