MissingSchemaError:架构没有被注册为模型,mongoose.model在架构之前调用
我得到这样的错误,这些是堆栈跟踪线,从开始:
这是我的主节点服务器文件,我加载路由
require('./config/routes')(app);
这是路由文件,将要求
var todos = require('../app/controllers/todos'); controller
这是控制器,在第2行给出错误:
var mongoose = require('mongoose'), Todo = mongoose.model('Todo'), _ = require('underscore');
这是我的模型文件,似乎没有被调用:
var mongoose = require('mongoose'), Schema = mongoose.Schema; var TodoSchema = new Schema({ created: { type: Date, default: Date.now }, content: { type: String, default: '', trim: true } }); TodoSchema.path("content").validate(function(content) { return content.length; }, 'Content cannot be blank'); mongoose.model("Todo", TodoSchema);
所以,如果我理解正确,我的问题是,这行(内部控制器)被称为:
Todo = mongoose.model('Todo')
在我的模型中的这一行之前:
mongoose.model("Todo", TodoSchema);
对? 不过,我从mean.io和其他几个人那里拿了一堆,然后把它放在一起。
让我们看看mean.io代码,他们正在做同样的事情:
server.js
require('./config/routes')(app, passport, auth);
然后进入routes.js和代码:
var articles = require('../app/controllers/articles');
然后进入物品pipe理员:
var mongoose = require('mongoose'), Article = mongoose.model('Article'), _ = require('underscore');
这工作正常,但它是完全一样的订单矿。
在文件server.js
( https://github.com/linnovate/mean/blob/master/server.js )中,有一个被调用来自动加载模型的函数:
//Bootstrap models var models_path = __dirname + '/app/models'; var walk = function(path) { fs.readdirSync(path).forEach(function(file) { var newPath = path + '/' + file; var stat = fs.statSync(newPath); if (stat.isFile()) { if (/(.*)\.(js$|coffee$)/.test(file)) { require(newPath); } } else if (stat.isDirectory()) { walk(newPath); } }); }; walk(models_path);
这会加载/app/models
目录中定义的所有/app/models
。 这将需要发生在你的控制器之前的模型types的引用。