Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findOne function queries for id and not the param #15306

Open
2 tasks done
JulSeb42 opened this issue Mar 7, 2025 · 0 comments
Open
2 tasks done

findOne function queries for id and not the param #15306

JulSeb42 opened this issue Mar 7, 2025 · 0 comments

Comments

@JulSeb42
Copy link

JulSeb42 commented Mar 7, 2025

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.12.1

Node.js version

20.18.0

MongoDB server version

7.0.14

Typescript version (if applicable)

5.8.2

Description

I'm having an issue with the findOne function. I'm trying to get some data with the slug, and I'm getting this error:

CastError: Cast to ObjectId failed for value "<slug>" (type string) at path "_id" for model "<model>"

Steps to Reproduce

This is the function I'm using:

router.get("/product-slug/:slug", (req, res, next) => {
    return ProductModel.findOne({ slug: req.params.slug })
        .then(foundProduct => res.status(200).json(foundProduct))
        .catch(err => next(err))
})

Expected Behavior

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant