22

When trying to create a model in Mongoose I get the following error

[TypeError: Cannot read property 'options' of undefined]

I have no idea what's causing it

"use strict";
var Step = require('step');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

function randomFunction() {
    var categorySchema = new Schema({
        id: Number,
        name: String,
        description: String
    }, { collection: 'categories' });

    var Category;

    //...

    mongoose.connect('mongodb://localhost/grouping');

    new Step(
        function() { //Connect to mongodb
            var db = mongoose.connection;
            db.on('error', console.error.bind(console, 'connection error:'));
            db.on('open', this);
        },
        function() {  //Create model
            console.log(categorySchema); //Logs the schema object right
            Category = mongoose.Model('Category', categorySchema);


        },
        function(err) {
            console.log(err);  //Error here
        });
    //...
}

I'm very new to Mongo (and fairly new to node) but I have absolutely no idea what the error message means.

I know I have options defined in the schema but I cant see how it would be undefined, can anyone point me in the right direction?

Note - this is a big cut out of the original code, this is the general structure (there's actually some code below mongoose.Model('Cat... but it gets skipped, I assume because the error is thrown by the mongoose.Model call as not even a console.log("Hello"); is printed straight after it.

EDIT I've found that inside Mongoose (mongoose/lib/document.js) tries to get this.schema but it's undefined

function Document (obj, fields, skipId) { //Line 37
    this.$__ = new InternalCache;
    this.isNew = true;
    this.errors = undefined;

    var schema = this.schema; //-> undefined
    // ...
6
  • Are you sure, you are connected to MongoDB, the right code for connecting is mongoose.connect('mongodb://host/database', function(err) { console.log('error occured', err); }); ? Apr 6, 2013 at 11:39
  • I'll try that, thanks. I found that other connection code somewhere which worked better for me because it was seperated
    – Sam
    Apr 6, 2013 at 11:44
  • Turns out I missed a line from my question, I'll add it in
    – Sam
    Apr 6, 2013 at 11:45
  • In that case would you mind putting full dump of the error ? Apr 6, 2013 at 11:50
  • Sure pastebin.com/uVSEanPH
    – Sam
    Apr 6, 2013 at 12:09

3 Answers 3

88

So it turns out I'm not the observent kind,

mongoose.Model should be mongoose.model

20
  • 24
    I, too, appear to be unobservant. Nov 1, 2013 at 16:15
  • 7
    I, five, appear to be unobservant.
    – Frosty619
    Jan 29, 2016 at 19:40
  • 6
    I, six, appear to be unobservant.
    – J.Steve
    Mar 25, 2016 at 1:20
  • 5
    I, seven, appear to be unobservant
    – Sherlock
    Apr 7, 2016 at 10:54
  • 7
    I, eleven, appear to be unobservant. Apr 20, 2016 at 14:52
3

You can also get the same error for calling this.

MyModel = new mongoose.model('<your model name>', mySchema)

if you do remove the new.

1

This error message will also be displayed when using model methods in a Promise chain such as:

const Product = mongoose.model('Product', ProductSchema)

ScrapProducts()
  .then(mapToModel)
  .then(Product.create)

To solve it you must ensure that your mongoose model preserves its original context.

const Product = mongoose.model('Product', ProductSchema)

ScrapProducts()
  .then(mapToModel)
  .then(function(data) {
    return Product.create(data)
  })

or better:

const Product = mongoose.model('Product', ProductSchema)

ScrapProducts()
  .then(mapToModel)
  .then(Product.create.bind(Product))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.