I've got a schema that includes dates:
const dateSchema = new mongoose.Schema({
resortName: {
type: String,
required: true,
},
date: {
type: Date,
required: true,
},
runName: {
type: String,
required: true,
},
weatherConditions: String,
windConditions: String,
snowConditions: String,
runTime: Number,
runCrowded: Boolean,
runJumps: Boolean,
runNotes: String,
});
And I'm seeding my DB with some dummy JSON:
[{
"resortName": "Winter Park",
"date": "2022-04-15T09:31:03.503Z",
"runName": "Mary Jane",
"runDifficulty": "blue",
"weatherConditions": "cloudy",
"windConditions": "windy",
"snowConditions": "icy",
"runTime": 13,
"runCrowded": true,
"runJumps": false,
"runNotes": "Crowded run, patches of ice in middle of slope"
}]
I created the following route:
dateRouter.get("/", (req, res) => {
Date.find({}).then((dates) => {
res.json(dates);
});
});
, but when I attempt to visit the route I get the following error:
C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\query.js:4697
const castError = new CastError();
^
CastError: Cast to ObjectId failed for value "{ _id: 'dates' }" (type Object) at path "_id" for model "Resort"
at model.Query.exec (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\query.js:4697:21)
at model.Query.Query.then (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\query.js:4796:15)
at C:\Users\jacob\Desktop\projects\slope-notes\controllers\resortControllers.js:21:23
at Layer.handle [as handle_request] (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\jacob\Desktop\projects\slope-notes\node_modules\express\lib\router\index.js:281:22
at param (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\express\lib\router\index.js:360:14)
at param (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\express\lib\router\index.js:371:14) {
messageFormat: undefined,
stringValue: `"{ _id: 'dates' }"`,
kind: 'ObjectId',
value: { _id: 'dates' },
path: '_id',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
at new BSONTypeError (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\bson\lib\error.js:41:28)
at new ObjectId (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\bson\lib\objectid.js:65:23)
at castObjectId (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\cast\objectid.js:19:14)
at ObjectId.cast (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\schema\objectid.js:247:12)
at ObjectId.SchemaType.applySetters (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\schematype.js:1189:12)
at ObjectId.SchemaType._castForQuery (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\schematype.js:1623:15)
at ObjectId.SchemaType.castForQuery (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\schematype.js:1613:15)
at ObjectId.SchemaType.castForQueryWrapper (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\schematype.js:1590:20)
at cast (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\cast.js:288:34)
at model.Query.Query.cast (C:\Users\jacob\Desktop\projects\slope-notes\node_modules\mongoose\lib\query.js:5119:12),
valueType: 'Object'
}
CodePudding user response:
first I believe you have a naming conflict, Date
is a reserved word in javascript/node, if you change the collection name the issue should resolve itself
but you may have a better experience with the following:
dateRouter.get("/", async (req, res) => {
// (change Date to a different name)
const records = await Date.find({})
res.json(records)
})