0
votes

when I use findOne from mongoose version 5.9.12, I got some error like this : error https://i.stack.imgur.com/8kAcY.png

my code: [code][2]

https://i.stack.imgur.com/v8Prd.png

my models: models

1
Please edit your question and add your code instead of posting images.eol

1 Answers

0
votes

From the mongoose documentation:

If you want to query by a document's _id, use findById() instead of findOne().

The id is cast based on the Schema before sending the command.

So the recommended way to do this is:

Category.findById(categoryId)

If you really want to use findOne and specify _id inside the filter, you can convert the string using

Category.findOne( { _id: mongoose.Types.ObjectId(categoryId) })

to an ObjectId.


Edit: The actual problem is that your categoryId contains a trailing whitespace which causes this problem: "5f05445a9789663d08fb12d2 " - it should be "5f05445a9789663d08fb12d2".