question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mongoose "instance" value representing the Date type is undefined in Schema object

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Jul 5, 2017

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.

$ node gh-1938.js 
Schema {
  obj: 
   { key: { type: [Function: String], unique: true, required: true },
     usedCount: { type: [Function: Number], default: 0 },
     maxUse: { type: [Function: Number], default: 1 },
     dateCreated: { type: [Function: Date], default: [Function: now] } },
  paths: 
   { key: 
      SchemaString {
        enumValues: [],
        regExp: null,
        path: 'key',
        instance: 'String',
        validators: [Object],
        setters: [],
        getters: [],
        options: [Object],
        _index: [Object],
        isRequired: true,
        requiredValidator: [Function],
        originalRequiredValue: true },
     usedCount: 
      SchemaNumber {
        path: 'usedCount',
        instance: 'Number',
        validators: [],
        setters: [],
        getters: [],
        options: [Object],
        _index: null,
        defaultValue: 0 },
     maxUse: 
      SchemaNumber {
        path: 'maxUse',
        instance: 'Number',
        validators: [],
        setters: [],
        getters: [],
        options: [Object],
        _index: null,
        defaultValue: 1 },
     dateCreated: 
      SchemaDate {
        path: 'dateCreated',
        instance: 'Date',
        validators: [],
        setters: [],
        getters: [],
        options: [Object],
        _index: null,
        defaultValue: [Function: now] },
     _id: 
      ObjectId {
        path: '_id',
        instance: 'ObjectID',
        validators: [],
        setters: [Object],
        getters: [],
        options: [Object],
        _index: null,
        defaultValue: [Function: defaultId] } },
  aliases: {},
  subpaths: {},
  virtuals: { id: VirtualType { path: 'id', getters: [Object], setters: [], options: {} } },
  singleNestedPaths: {},
  nested: {},
  inherits: {},
  callQueue: [ [ 'pre', [Object] ] ],
  _indexes: [],
  methods: {},
  statics: {},
  tree: 
   { key: { required: true, unique: true, type: [Function: String] },
     usedCount: { default: 0, type: [Function: Number] },
     maxUse: { default: 1, type: [Function: Number] },
     dateCreated: { default: [Function: now], type: [Function: Date] },
     _id: { type: [Object], auto: true },
     id: VirtualType { path: 'id', getters: [Object], setters: [], options: {} } },
  query: {},
  childSchemas: [],
  plugins: [],
  s: 
   { hooks: Kareem { _pres: {}, _posts: {} },
     kareemHooks: 
      { count: true,
        find: true,
        findOne: true,
        findOneAndUpdate: true,
        findOneAndRemove: true,
        insertMany: true,
        update: true,
        updateMany: true,
        updateOne: true } },
  options: 
   { retainKeyOrder: false,
     typeKey: 'type',
     id: true,
     noVirtualId: false,
     _id: true,
     noId: false,
     validateBeforeSave: true,
     read: null,
     shardKey: null,
     autoIndex: null,
     minimize: true,
     discriminatorKey: '__t',
     versionKey: '__v',
     capped: false,
     bufferCommands: true,
     strict: true } }
$ 
1reaction
gregksurveilluscommented, Jun 23, 2017

And it’s still “broken” in the 5.03 release as of today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Mongoose instance method returning undefined
I am trying to parse and save a name field into separate first and last names for a mongoose User model. My model...
Read more >
Mongoose v6.8.2: SchemaTypes
You can declare a schema type using the type directly, or an object with a type property. const schema1 = new Schema({ test:...
Read more >
My mongoose Schema variables are coming back undefined ...
undefined undefined Error: data and salt arguments required at Object.hash ... Schema({ userName: { type : String, unique : true, ...
Read more >
Smarter way of using MongoDB with Node.js - Mongoose
Schema is nothing but the JSON representation of the data that is going to be stored in the collections. It allows you to...
Read more >
types/mongoose/index.d.ts - UNPKG
The CDN for @types/mongoose. ... 142, // otherwise if it's an object or undefined (for optional props), ... 343, * @param base a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found