Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

MongoDB Nextjs Data Serialization

I'm trying to load data from mongodb into nextjs but keep recieving this error. The data has been entered the database by Python pymongo.

SerializableError: Error serializing .jobs[0] returned from getServerSideProps in "/". Reason: undefined cannot be serialized as JSON. Please use null or omit this value.

enter image description here

Here is my function:

export async function getServerSideProps(context) {
  const { db } = await connectToDatabase();

  const data = await db
    .collection("alljobs")
    .find()
    .sort({ _id: 1 })
    .limit(40)
    .toArray();

  const jobs = data.map((job) => {
    const company = JSON.parse(JSON.stringify(job.company));
    const impact = JSON.parse(JSON.stringify(job.impact));
    const date = JSON.parse(JSON.stringify(job.date));
    const role = JSON.parse(JSON.stringify(job.role));
    const location = JSON.parse(JSON.stringify(job.location));
    const category = JSON.parse(JSON.stringify(job.category));
    const link = JSON.parse(JSON.stringify(job.link));

    return;

    {
      company: company;
      impact: impact;
      date: date;
      role: role;
      location: location;
      category: category;
      link: link;
    }
  });

  return {
    props: { jobs },
  };
}
question from:https://stackoverflow.com/questions/65644036/mongodb-nextjs-data-serialization

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...