I have a problem with NextJS. I get an error 'can`t resolve DNS' when I use getStaticProps and working with MongoDb. Why does this happen? Error occurs only when I have MongoDB code. Also code works in NextPages but not in React components.
export const getStaticProps: GetStaticProps = async (context) => {
const client = await MongoClient.connect(
"mongodb+srv://maciejtest:password0.vv1w4.mongodb.net/stockystocks?retryWrites=true&w=majority"
);
const db = client.db();
const detailedAssetsCollection = db.collection("detailedAssets");
const detailedAssets = await detailedAssetsCollection.find({}).toArray();
console.log(detailedAssets);
return {
props: {
assets: detailedAssets,
},
};
};
liborutilfolder (outside ofpage) and import a connect utility function. See the official Nextjs with-mogodb example.getStaticPropsonly works in page components, it doesn't run in regular components. That would explain the error since the imports for the server-side code aren't being used, so Next.js also includes them in the client bundle.