I'm new to next.js. In the following code, I'm rendering the home page on the server and populating the props object through a file named "products.json". I have successfully done that. now the next step for me is to populate the props object, in the function named getStaticProps, through MongoDB.
CAN I CONNECT TO MY MongoDB USING MONGOOSE? if not, please guide me with the best approach to do so.
import fs from "fs/promises";
import path from "path";
import Link from "next/link";
const Home = (props) => {
const { products } = props;
return (
<div>
<div>Lists of product:</div>
<ul>
{products.map((product) => {
return (
<li key={product.id}>
<Link href={`/${product.id}`}>{product.title}</Link>
</li>
);
})}
</ul>
</div>
);
};
export async function getStaticProps() {
console.log("Re-Generating...");
const filePath = path.join(process.cwd(), "data", "products.json");
const jsonData = await fs.readFile(filePath);
const data = JSON.parse(jsonData);
return {
props: data,
revalidate: 10,
};
}
export default Home;