'How to query MongoDb with a different field than _id?
I'd like to retrieve an object from MongoDB other than its _id. I'm using it with react, axios and express. When I try this, I just get an error Error: Request failed with status code 500. How could I fix this? This is my code:
Express:
router.get("/page/:slug", (req, res, next) => {
Page.findOne({ slug: req.params.slug })
.then(foundPage => res.status(200).json(foundPage))
.catch(err => next(err))
})
Axios get:
pageSlug(slug) {
return axios.get(`http://localhost:5005/api/pages/page/${slug}`)
}
And JSX:
const { slug } = useParams()
const [page, setPage] = useState()
useEffect(() => {
pageSlug(slug)
.then(res => {
setPage(res.data)
})
.catch(err => console.log(err))
}, [])
Thanks for your help!
Edit: this is the error I'm getting in the terminal:
ERROR GET /api/pages/page/about CastError: Cast to ObjectId failed for value "about" (type string) at path "_id" for model "Page"
at model.Query.exec (/Users/user/Documents/Github/blog-new/node_modules/mongoose/lib/query.js:4641:21)
at model.Query.Query.then (/Users/user/Documents/Github/blog-new/node_modules/mongoose/lib/query.js:4740:15)
at /Users/user/Documents/Github/blog-new/routes/pages.js:17:10
at Layer.handle [as handle_request] (/Users/user/Documents/Github/blog-new/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/user/Documents/Github/blog-new/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/user/Documents/Github/blog-new/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/user/Documents/Github/blog-new/node_modules/express/lib/router/layer.js:95:5)
at /Users/user/Documents/Github/blog-new/node_modules/express/lib/router/index.js:281:22
at param (/Users/user/Documents/Github/blog-new/node_modules/express/lib/router/index.js:360:14)
at param (/Users/user/Documents/Github/blog-new/node_modules/express/lib/router/index.js:371:14) {
messageFormat: undefined,
stringValue: '"about"',
kind: 'ObjectId',
value: 'about',
path: '_id',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters
at new BSONTypeError (/Users/user/Documents/Github/blog-new/node_modules/bson/lib/error.js:41:28)
at new ObjectId (/Users/user/Documents/Github/blog-new/node_modules/bson/lib/objectid.js:65:23)
at castObjectId (/Users/user/Documents/Github/blog-new/node_modules/mongoose/lib/cast/objectid.js:24:12)
at ObjectId.cast (/Users/user/Documents/Github/blog-new/node_modules/mongoose/lib/schema/objectid.js:247:12)
at ObjectId.SchemaType.applySetters (/Users/user/Documents/Github/blog-new/node_modules/mongoose/lib/schematype.js:1180:12)
at ObjectId.SchemaType._castForQuery (/Users/user/Documents/Github/blog-new/node_modules/mongoose/lib/schematype.js:1614:15)
at ObjectId.SchemaType.castForQuery (/Users/user/Documents/Github/blog-new/node_modules/mongoose/lib/schematype.js:1604:15)
at ObjectId.SchemaType.castForQueryWrapper (/Users/user/Documents/Github/blog-new/node_modules/mongoose/lib/schematype.js:1581:20)
at cast (/Users/user/Documents/Github/blog-new/node_modules/mongoose/lib/cast.js:344:32)
at model.Query.Query.cast (/Users/user/Documents/Github/blog-new/node_modules/mongoose/lib/query.js:5076:12),
valueType: 'string'
}
Edit 2: this is the model:
const pageSchema = new Schema(
{
...other stuff
slug: {
type: String,
unique: true,
},
},
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
