【问题标题】:Coffee-Script Nodejs and MongooseCoffee-Script Nodejs 和 Mongoose
【发布时间】:2012-04-27 13:46:16
【问题描述】:

大家都在尝试使用 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);
    });
  }
};

【问题讨论】:

  • 生成的javascript是什么样的?
  • 我已经为你添加了问题:o)

标签: node.js mongodb coffeescript express mongoose


【解决方案1】:

我相信mongoose.model 不是构造函数。我已经有一段时间没有使用猫鼬了,但我也不记得Schema.add,这些字段应该传递给构造函数调用。所以它看起来像这样:

Quote = new Schema
    quote : { type: String, required: true }
    rank  : Number

quote = mongoose.model 'Quote', Quote

editSchema.add 很好,它实际上是从构造函数中调用的。它只是在文档中简要提及。

【讨论】:

  • 谢谢。新的通过是问题所在。 :D
猜你喜欢
  • 2015-07-19
  • 1970-01-01
  • 2015-03-13
  • 2014-08-08
  • 1970-01-01
  • 2012-10-14
  • 2015-01-08
  • 2014-05-16
  • 1970-01-01
相关资源
最近更新 更多