У меня есть схема:
const {Schema, model} = require("mongoose")
const ProductSchema = new Schema({
title: {
type: String
},
price: {
type:Number,
min:0,
required:true
},
description: {
type: String
},
category: {
type: String
},
img:{
type:String
}
})
module.exports = model('Product', ProductSchema)
Есть кнопка с который я получаю req параметр:
<div class="sort">
<a class="btn-sort" href="/product/birthdays">Дни рождения</a>
</div>
И передаю все это в контроллер:
const getProductCategory = (req, res) => {
const title = "Дни рождения"
Products.find({category:req.params.category})
.then((productscategory) => console.log(productscategory))
.catch((error) => {
console.log(error);
})
}
Но при нажатии получаю ошибку, и я не понимаю что делать.
CastError: Cast to ObjectId failed for value "birthdays" (type string) at path "_id" for model "Product"
at model.Query.exec (F:\Project\testproject\node_modules\mongoose\lib\query.js:4584:21)
at model.Query.Query.then (F:\Project\testproject\node_modules\mongoose\lib\query.js:4683:15)
at getProduct (F:\Project\testproject\controllers\productController.js:7:6)
at Layer.handle [as handle_request] (F:\Project\testproject\node_modules\express\lib\router\layer.js:95:5)
at next (F:\Project\testproject\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (F:\Project\testproject\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (F:\Project\testproject\node_modules\express\lib\router\layer.js:95:5)
at F:\Project\testproject\node_modules\express\lib\router\index.js:281:22
at param (F:\Project\testproject\node_modules\express\lib\router\index.js:354:14)
at param (F:\Project\testproject\node_modules\express\lib\router\index.js:365:14) {
messageFormat: undefined,
stringValue: '"birthdays"',
kind: 'ObjectId',
value: 'birthdays',
path: '_id',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters
at new BSONTypeError (F:\Project\testproject\node_modules\bson\lib\error.js:41:28)
at new ObjectId (F:\Project\testproject\node_modules\bson\lib\objectid.js:65:23)
at castObjectId (F:\Project\testproject\node_modules\mongoose\lib\cast\objectid.js:25:12)
at ObjectId.cast (F:\Project\testproject\node_modules\mongoose\lib\schema\objectid.js:245:12)
at ObjectId.SchemaType.applySetters (F:\Project\testproject\node_modules\mongoose\lib\schematype.js:1135:12)
at ObjectId.SchemaType._castForQuery (F:\Project\testproject\node_modules\mongoose\lib\schematype.js:1567:15)
at ObjectId.SchemaType.castForQuery (F:\Project\testproject\node_modules\mongoose\lib\schematype.js:1557:15)
at ObjectId.SchemaType.castForQueryWrapper (F:\Project\testproject\node_modules\mongoose\lib\schematype.js:1534:20)
at cast (F:\Project\testproject\node_modules\mongoose\lib\cast.js:336:32)
at model.Query.Query.cast (F:\Project\testproject\node_modules\mongoose\lib\query.js:5007:12),
valueType: 'string'
}