Mongoose Schema 中带有日期的 CastError

CastError with dates in Mongoose Schema

我有一个包含日期的架构:

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,
});

并且我正在为我的数据库播种一些虚拟 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"
}]

我创建了以下路线:

dateRouter.get("/", (req, res) => {
  Date.find({}).then((dates) => {
    res.json(dates);
  });
});

,但是当我尝试访问路线时出现以下错误:

 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'
}

首先我认为您有命名冲突,Date 是 javascript/node 中的保留字,如果您更改集合名称,问题应该会自行解决

但您可能对以下内容有更好的体验:


dateRouter.get("/", async (req, res) => {
  // (change Date to a different name)
  const records = await Date.find({})
  res.json(records)
})