how to use findById
on Next.js
#11834
Answered
by
vkarpov15
SanityZeroPercent
asked this question in
Q&A
-
Hey guys, I'm a newbie on mongoose. So basically, i want to find by id on next.js but it fails miserably. Here's my code: import Link from 'next/link'
import dbConnect from '../../lib/dbConnect'
import Levelling from '../../models/Levelling'
/* Allows you to view user card info*/
const LevellingPage = ({ pet }) => {
return (
<div key={pet._id}>
<div className="card">
<img
src={pet.image_url}
/>
<h3 className="fpanel-name">{pet.displayname}</h3>
<div className="main-content">
<p className="displayname">{pet.displayname}</p>
<p className="levels">Level {pet.level}</p>
<p className="stats">Total XP: {pet.formatxp}</p>
<p className="stats">Total Message: {pet.formatmessage}</p>
<div className="btn-container">
<Link href="/">
<button className="btn view">Go Back</button>
</Link>
</div>
</div>
</div>
</div>
)
}
export async function getServerSideProps({ params }) {
await dbConnect()
const pet = await Levelling.findById(params.id).lean()
pet._id = pet._id.toString()
return { props: { pet } }
}
export default LevellingPage and this is the error i got:
Please help me, thank you 🙏 |
Beta Was this translation helpful? Give feedback.
Answered by
vkarpov15
Jun 7, 2022
Replies: 1 comment 1 reply
-
See vercel/next.js#11993 . Do |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
SanityZeroPercent
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See vercel/next.js#11993 . Do
return { props: { pet: JSON.parse(JSON.stringify(pet)) } }
instead. NextJS requires you to explicitly convertpet
into an object that is representable in JSON beforehand. So noDate
objects, no ObjectIds, etc.