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
// ...
So it turns out I'm not the observent kind,
mongoose.Model should be mongoose.model
You can also get the same error for calling this.
MyModel = new mongoose.model('<your model name>', mySchema)
if you do remove the new.