I want to pass the user data I fetched from a MongoDB database using the user id to the props in the return section. how can I send the data docs
in the callback function to the props
correctly?
Thanks for your response.
export const getServerSideProps = async (userid) => {
await connectMongo();
console.log("db connected on profile page");
User.findOne({ id: userid }, function (err, docs) {
if (err) {
console.log(err);
} else {
console.log(docs.email);
}
});
return {
props: {
},
};
};
2
Answers
you can create a new variable to solve the problem
there is another solution by using
async
. the code be like:I have used MongoDB in the frontend as well, I have added a snippet to give a full example. However simply put you’ll need to store the result of the DB find request in a variable and if you’re just searching with a
findOne()
or looking for one result I suggest storing the result of the find in a variable and sending it directly to the page as a prop.However you will need to call
JSON.parse(JSON.stringify(results))
if you have multiple results from adb.collection("submissions").find()
request.Client Side /page/…
clientPromise