node.js MissingSchemaError:尚未为模型注册架构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18420920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MissingSchemaError: Schema hasn't been registered for model
提问by Andres Vargas
I have a typical Project with Node.js - Express 3 - MongoDB
我有一个典型的 Node.js 项目 - Express 3 - MongoDB
I'm trying to make a query to my model 'Tweet' in my /routes/index.js and when I run my app crashed
我试图在我的 /routes/index.js 中查询我的模型“Tweet”,当我运行我的应用程序时崩溃了
24 Aug 11:35:07 - [nodemon] starting `node app.js`
/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/node_modules/mongoose/lib/index.js:286
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Teewt".
Use mongoose.model(name, schema)
at Mongoose.model (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/node_modules/mongoose/lib/index.js:286:13)
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/routes/index.js:6:33)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/app.js:7:14)
at Module._compile (module.js:456:26)
24 Aug 11:35:07 - [nodemon] app crashed - waiting for file changes before starting...
This is part of my app.js
这是我的 app.js 的一部分
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/fanOcesa');
var Schema = mongoose.Schema;
var ObjectID = Schema.ObjectID;
var Teewt = new Schema({
cuerpo: String
});
var Teewt = mongoose.model('Teewt', Teewt);
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
And this is part of my index.js
这是我的 index.js 的一部分
var Teewt = require('mongoose').model('Teewt');
Teewt.find({}, function(err, docs){
console.log('docs');
console.log(docs);
});
exports.index = function(req, res){
res.render('index', {
docs: docs
});
};
which would be the correct way to do this query?
这将是执行此查询的正确方法?
回答by JohnnyHK
The index.jsfile is executed where your app.jsfile calls:
该index.js文件在您的app.js文件调用的地方执行:
var routes = require('./routes');
So be sure that's being called afteryour calls to register the 'Teewt'schema as a mongoose model in app.js.
因此,请确保在您调用将'Teewt'架构注册为app.js.
回答by Peter Lyons
Name your schema and model differently. Re-declaring Teewtis a javascript "bad part" as well as a mistake in any programming language. Just call the schema TeewtSchemaand the model Teewt(since the schema is typically only used in 1 file in an application, but the model may be used extensively).
以不同的方式命名您的架构和模型。重新声明Teewt是 javascript 的“坏部分”,也是任何编程语言中的错误。只需调用架构TeewtSchema和模型Teewt(因为架构通常仅在应用程序的 1 个文件中使用,但模型可能会被广泛使用)。
回答by Avik Ray
I got this error too. Was baffled for long, following this and similar threads for help, trying to debug. Ultimately the issue turned out to be due to mongoose version in my case. I was trying to use mongoose-fixture to seed some default data to start with into mongo.
我也收到了这个错误。困惑了很长时间,跟随这个和类似的线程寻求帮助,试图调试。最终,在我的情况下,问题是由于猫鼬版本造成的。我试图使用 mongoose-fixture 将一些默认数据播种到 mongo 中。
Try this: delete the node_modulesdir of your project, run an npm cache clean, and then an npm install. If even this doesn't help, try comparing versions of mongoose/ mongoose-fixturebetween the problematic app and one that works, and try changing the version in your package.json, and repeating the above steps. This worked for me.
试试这个:删除node_modules项目的目录,运行一个npm cache clean,然后运行一个npm install. 如果即使这样也无济于事,请尝试比较有问题的应用程序和有效应用程序之间的mongoose/版本mongoose-fixture,并尝试更改 中的版本package.json,并重复上述步骤。这对我有用。
回答by Aviram Netanel
in other words, this line (from app.js):
换句话说,这一行(来自 app.js):
var Teewt = mongoose.model('Teewt', Teewt);
should be called after the Tweet was registered, on that line (in index.js):
应该在 Tweet 注册后在该行(在 index.js 中)调用:
var Teewt = require('mongoose').model('Teewt');

