Mongoose: TypeError: Cannot read property 'options' of undefined

Created on 7 Apr 2014  路  7Comments  路  Source: Automattic/mongoose

mongoose 3.8.8

the following error is thrown when I update a PoTranslation model with the following schema:

var History = new Schema({
msgStr: String,
createdAt: Date,
createdBy: {
type: String,
ref: 'User'
}
});

var PoTranslation = new Schema({
histories: [History]
});

[TypeError: Cannot read property 'options' of undefined]
TypeError: Cannot read property 'options' of undefined
at SchemaString.cast (/projects/iyaboo/node_modules/mongoose/lib/schema/string.js:279:22)
at /projects/iyaboo/node_modules/mongoose/lib/document.js:288:29
at EmbeddedDocument.Document.$__try (/projects/iyaboo/node_modules/mongoose/lib/document.js:757:8)
at init (/projects/iyaboo/node_modules/mongoose/lib/document.js:287:16)
at EmbeddedDocument.Document.init (/projects/iyaboo/node_modules/mongoose/lib/document.js:246:3)
at DocumentArray.cast (/projects/iyaboo/node_modules/mongoose/lib/schema/documentarray.js:135:27)
at /projects/iyaboo/node_modules/mongoose/lib/document.js:288:29
at model.Document.$__try (/projects/iyaboo/node_modules/mongoose/lib/document.js:757:8)
at init (/projects/iyaboo/node_modules/mongoose/lib/document.js:287:16)
at model.Document.init (/projects/iyaboo/node_modules/mongoose/lib/document.js:246:3)
at completeOne (/projects/iyaboo/node_modules/mongoose/lib/query.js:1392:10)
at Promise. (/projects/iyaboo/node_modules/mongoose/lib/query.js:1160:11)
at Promise. (/projects/iyaboo/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
at Promise.EventEmitter.emit (events.js:95:17)
at Promise.emit (/projects/iyaboo/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
at Promise.fulfill (/projects/iyaboo/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
at Promise.resolve (/projects/iyaboo/node_modules/mongoose/lib/promise.js:108:15)
at /projects/iyaboo/node_modules/mongoose/lib/model.js:2018:23
at process._tickCallback (node.js:415:13)

Most helpful comment

@kantlove Should be model instead of Model : http://mongoosejs.com/docs/models.html

All 7 comments

Im getting the same error too, using 4.9.7 and nothing wrong with my code.

`\node_modules\mongoose\lib\document.js:51
this.$__.strictMode = schema.options && schema.options.strict;
^

TypeError: Cannot read property 'options' of undefined
at Document (C:\Users\Ash-B\Documents\GitHub\Nodejs-Account-System\node_modules\mongoose\lib\document.js:51:33)
at Model (C:\Users\Ash-B\Documents\GitHub\Nodejs-Account-System\node_modules\mongoose\lib\model.js:45:12)
at Object. (C:\Users\Ash-B\Documents\GitHub\Nodejs-Account-System\src\models\users.js:36:29)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (C:\Users\Ash-B\Documents\GitHub\Nodejs-Account-System\src\server.js:25:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3`

I have the same error with Mongoose v4.11.7 in es6. Here is my code:

import mongoose from 'mongoose';

const user_schema = mongoose.Schema({
  name: String
});

export default new mongoose.Model('User', user_schema);

Why do you have the new in export default new mongoose.Mode('User', ...) ?

@kantlove Should be model instead of Model : http://mongoosejs.com/docs/models.html

Good catch @EtienneR , will add an error for that

this.$__.strictMode = schema.options && schema.options.strict;
////can't found any way to fix this problem

@EtienneR That was apt! I was typing Model instead of model.

Was this page helpful?
0 / 5 - 0 ratings