Hey all trying to play with nodejs MVC using express and mongoose.
I have the following code:
mongoose = require 'mongoose'
Schema = mongoose.Schema
mongoose.connect 'mongodb://localhost/quotes'
Quote = new Schema()
Quote.add {
quote : { type: String, required: true}
, rank : Number
}
quote = new mongoose.model 'Quote', Quote
module.exports =
index: (req,res, next) ->
quote.find {}, (err,docs) ->
res.render docs
When I go to run the server, I receive the following:
Server started on port 3000
module.js:311
throw err;
^
TypeError: Cannot read property 'Quote' of undefined
at new <anonymous> (/Users ....
I can't work out why it keeps throwing the undefined error.
EDIT:
Here is the compiled JS
var Quote, Schema, mongoose, quote;
mongoose = require('mongoose');
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/quotes');
Quote = new Schema();
Quote.add({
quote: {
type: String,
required: true
},
rank: Number
});
quote = new mongoose.model('Quote', Quote);
module.exports = {
index: function(req, res, next) {
return quote.find({}, function(err, docs) {
return res.render(docs);
});
}
};
I believe mongoose.model
is not a constructor. It's been a while since I've used mongoose, but I don't remember Schema.add
either, the fields should be passed to the constructor call. So it would look like this:
Quote = new Schema
quote : { type: String, required: true }
rank : Number
quote = mongoose.model 'Quote', Quote
edit: Schema.add
is fine, it's actually called from the constructor. It's just briefly mentioned in the docs.