Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose error on creating Model (using Step)

Tags:

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     // ... 
like image 453
Sam Avatar asked Apr 05 '13 18:04

Sam


People also ask

What is the difference between a mongoose schema and model?

A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

Does Mongoose create collection automatically?

Here mongoose will check if there is a collection called "Users" exists in MongoDB if it does not exist then it creates it. The reason being, mongoose appends 's' to the model name specified. In this case 'User' and ends up creating a new collection called 'Users'.

Does Mongoose throw error?

When we run code to save or update the data, all validator runs and if any issue is found by the validator, mongoose throws the error. But these errors are completely different, different validators send errors in different ways.

What problem does Mongoose JS solve?

Mongoose is a Node. js-based Object Data Modeling (ODM) library for MongoDB. It is akin to an Object Relational Mapper (ORM) such as SQLAlchemy for traditional SQL databases. The problem that Mongoose aims to solve is allowing developers to enforce a specific schema at the application layer.


2 Answers

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

mongoose.Model should be mongoose.model

like image 59
Sam Avatar answered Oct 22 '22 14:10

Sam


You can also get the same error for calling this.

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

if you do remove the new.

like image 43
CharlesTWall3 Avatar answered Oct 22 '22 14:10

CharlesTWall3