Hello there,
With the evolution of my website, I need to fetch data slices from Prismic and Data from my MongoDb database on my Homepage.
However, I get "Error: Your getServerSideProps
function did not return an object. Did you forget to add a return
?" when trying to add the mongoDb client in the getServerSideProps async function and I dont understand why. Could anybody help me on that matter please?
import React from "react";
import Head from "next/head";
import HeroHome from "../components/ui/HomePage/HeroHome";
// import NewsletterAlertsCta from "../components/ui/Shared/NewsletterAlertsCta";
import { useRouter } from "next/router";
import { SliceZone } from "@prismicio/react";
import { createClient } from "../prismicio";
import { components } from "../slices";
import clientPromise from "../lib/mongodb";
// import Script from "next/script";
import JobsFiltering from "../components/ui/Shared/Filtering/JobsFiltering";
const Homepage = (props) => {
const router = useRouter();
const currentRoute = router.pathname;
const {
metaTitle,
metaDescription,
ogImage,
pagePrismic,
navigation,
settings,
jobs,
totalPages,
page,
} = props;
console.log(jobs);
function structuredDataOrg() {
return {
__html: `{
"@context": "https://schema.org",
"@type": "Corporation",
"name": "Dataai Jobs",
"url": "https://dataai-jobs.com/",
"logo": "https://res.cloudinary.com/dl4mhtc4y/image/upload/v1668117398/dataai-jobs-logo-transparent_dxx3g4.png"
}`,
};
}
return (
<React.Fragment>
<Head>
<title>{metaTitle}</title>
<meta name="description" content={metaDescription} />
<link rel="canonical" href="/" key="canonical" />
{/* <!-- Twitter Card data --> */}
<meta name="twitter:card" content="summary" />
{/* <meta name="twitter:site" content="@publisher_handle" /> */}
<meta name="twitter:title" content={metaTitle} />
<meta name="twitter:description" content={metaDescription} />
{/* <meta name="twitter:creator" content="@author_handle" /> */}
{/* <-- Twitter Summary card images must be at least 120x120px --> */}
<meta name="twitter:image" content={ogImage} />
{/* <!-- Open Graph data --> */}
<meta property="og:title" content={metaTitle} />
<meta property="og:type" content="page" />
<meta property="og:url" content={currentRoute} />
<meta property="og:image" content={ogImage} />
<meta property="og:description" content={metaDescription} />
<meta property="og:site_name" content="Dataai Jobs" />
<script
type="application/ld+json"
dangerouslySetInnerHTML={structuredDataOrg()}
key="org-jsonld"
/>
</Head>
<HeroHome />
<JobsFiltering jobs={jobs} />
<SliceZone slices={pagePrismic.data.slices} components={components} />
{/* <NewsletterAlertsCta /> */}
</React.Fragment>
);
};
export default Homepage;
export async function getServerSideProps({ previewData }, context) {
try {
// Prismic client
const clientPrismic = createClient({ previewData });
const pagePrismic = await clientPrismic.getSingle("homepage");
// MongoDB
const clientMongoDb = await clientPromise;
const mongoDb = clientMongoDb.db("main");
const pageSize = 20;
// How to get to context.query? previewData.query?
let { page } = context.query;
page = page || 1;
// console.log("Page: " + page);
const jobs = await mongoDb
.collection("jobs")
.find({})
.sort({})
.skip((page - 1) * pageSize)
.limit(pageSize)
.toArray();
const totalJobs = await mongoDb.collection("jobs").count();
const totalPages = Math.ceil(totalJobs / pageSize);
// console.log("total pages in jobs: " + totalPages);
return {
props: {
metaTitle: page.data.meta_title,
metaDescription: page.data.meta_description,
ogImage: page.data.og_image.url,
pagePrismic: pagePrismic,
totalPages: totalPages,
page: page,
jobs: JSON.parse(JSON.stringify(jobs)),
},
};
} catch (e) {
console.log(e);
}
}