maria
maria

Reputation: 159

Nodejs include Models

Im having problems to make my server.js use my models.

I do have other models with same structure, like for example:

    var Post = mongoose.model('Post');
var Comment = mongoose.model('Comment');

but they works fine.. somehow.

But if If i try to use:

var Crime = mongoose.model('Crime');

i get:

         throw new mongoose.Error.MissingSchemaError(name);
      ^
MissingSchemaError: Schema hasn't been registered for model "Crime".

if i switch to:

var Crime = require('./models/Crime');

or

var Crime = require('./models/Crime.js');

i get the response:

Cannot find module './models/Crime'

Crime model code:

  var mongoose = require('mongoose');



var CrimeSchema = new mongoose.Schema({
  username: {type: String, lowercase: true, unique: true},
  time: String,
  salt: String
});

CrimeSchema.methods.performCrime = function(pass) {

    /*var deferred = $q.defer();


    deferred.notify('loading....');
     deferred.reject('Greeting is not allowed.');


  return deferred.promise;
  */

  return 22;
};


mongoose.model('Crime', CrimeSchema);

EDIT

example of working model:

var Post = mongoose.model('Post');

and post.js model

var mongoose = require('mongoose');

var PostSchema = new mongoose.Schema({
  title: String,
  link: String,
  upvotes: {type: Number, default: 0},
  comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});

PostSchema.methods.upvote = function(cb) {
  this.upvotes += 1;
  this.save(cb);
};

PostSchema.methods.downvote = function (cb) {
  this.upvotes -= 1;
  this.save(cb);
};

mongoose.model('Post', PostSchema);

Upvotes: 0

Views: 614

Answers (1)

John
John

Reputation: 4981

You forgot to export the module at the end.

var Crime = mongoose.model('Crime', CrimeSchema);    
module.exports = Crime;

Upvotes: 2

Related Questions