mongoose: Mongoose "instance" value representing the Date type is undefined in Schema object
I am programmatically creating CRUD methods for mongoose schemas when I noticed something strange. It seems for Date types in mongoose that the “instance” value in the exported schema object is undefined where others are correctly defined (such as Number, ObjectID, etc.).
I am not sure if this is a bug, or if maybe you could offer an explanation as to why mongoose works this way.
In the example below, you can see that dateCreated is of Date type, but the “instance” property value is undefined.
var MySchema = new Schema({
key: { type: String, unique: true, required: true },
usedCount: { type: Number, default: 0 },
maxUse: { type: Number, default: 1},
dateCreated: { type: Date, default: Date.now }
});
console.log(MySchema.paths);
Console Output:
{ key:
{ enumValues: [],
regExp: null,
path: 'key',
instance: 'String',
validators: [ [Object] ],
setters: [],
getters: [],
options: { type: [Function: String], unique: true, required: true },
_index: { unique: true },
isRequired: true },
usedCount:
{ path: 'usedCount',
instance: 'Number',
validators: [],
setters: [],
getters: [],
options: { type: [Function: Number], default: 0 },
_index: null,
defaultValue: 0 },
maxUse:
{ path: 'maxUse',
instance: 'Number',
validators: [],
setters: [],
getters: [],
options: { type: [Function: Number], default: 1 },
_index: null,
defaultValue: 1 },
dateCreated:
{ path: 'dateCreated',
instance: undefined,
validators: [],
setters: [],
getters: [],
options: { type: [Function: Date], default: [Function: now] },
_index: null,
defaultValue: [Function: now] },
_id:
{ path: '_id',
instance: 'ObjectID',
validators: [],
setters: [ [Function: resetId] ],
getters: [],
options: { type: [Function: ObjectId], auto: true },
_index: null,
defaultValue: [Function: defaultId] } }
_id: ObjectID
name: String
queryName: String
index: Number
_id: ObjectID
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 19 (3 by maintainers)
Issue looks fixed to me @gregksurveillus , here’s the output of running the original code for me with mongoose 4.11.1. Not sure what “release 5.03” means to you, mongoose doesn’t have such a release.
And it’s still “broken” in the 5.03 release as of today.