嘿,所有人都在尝试使用 express 和 mongoose 来玩 nodejs MVC。
我有以下代码:
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
当我去运行服务器时,我收到以下信息:
Server started on port 3000
module.js:311
throw err;
^
TypeError: Cannot read property 'Quote' of undefined
at new <anonymous> (/Users ....
我无法弄清楚为什么它一直抛出未定义的错误。
编辑:
这是编译好的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);
});
}
};