findOne cannot match id's as they are of different type

I am creating a [id].js dynamic route to serve blog data. I am using getStaticProps method to call to the database and findOne in collection. However, when I try to findOne and match id’s, I am comparing _id (which is an object), and params.id (which is a string).

const blog = await db.collection("blog").findOne({ _id: params.id})

As of now, blog returns NULL as expected since they do not match (type).

I have tried converting params.id to an object JSON.parse(params.id) but it gives an error:

SyntaxError: Unexpected token f in JSON at position 3

Before, I believe ObjectID was used to convert the string into an object. However, that was deprecated.

Anyone have any idea on how I should implement the [id].js route?

Code below for reference

export async function getStaticProps({params}) {
const client = await clientPromise
const db = await client.db('website')

const blog = await db
.collection("blog")
.findOne({
    _id: params.id
})

return {
    props: {
       blogpost: JSON.parse(JSON.stringify(blog))
},
}

Hi @Eugene_Baek ,

I think just convert the string to ObjectId before the query:

const myid = new ObjectId(params.id);
const blog = await db
.collection("blog")
.findOne({
    _id: myid
})

Thanks
Pavel

Thanks Pavel. I was confused as ObjectID was deprecated and replaced by ObjectId.