MissingSchemaError:架构尚未注册模型“用户”

在我的models/user.js文件中:

 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var userSchema = new Schema({ (define schema) }); ... (save user) ... (check password) ... mongoose.model('User', userSchema); 

而在我的router/index.js ,我有:

 var mongoose = require('mongoose'); var User = mongoose.model('User'); 

这会引发错误:

 MissingSchemaError: Schema hasn't been registered for model "User". 

但是,如果在user.js ,我(在最后一行)

 module.exports = mongoose.model('User', userSchema); 

并在index.jsvar User = require('../models/User'); ,那么一切正常。

但它不应该,因为在config/pass.js我正在做var User = mongoose.model('User'); config/pass.js var User = mongoose.model('User'); 它的工作完美无瑕。

require('../models/User'); 语法不适用于Ubuntu,但在我的Mac上。

我该怎么办? 我如何解决它? 我看了很多示例应用程序,包括MEAN,但没有什么是真正有用的。

当我尝试MEAN教程时,我遇到了同样的问题。

在做了一些研究之后,我发现在app.js中,如果我在var routes = require(“./ routes / index”)之前放入require(“./ models / User”),那么它就起作用了。

喜欢这个:


 mongoose.connect("mongodb://localhost/news"); require("./models/Posts"); require("./models/Comments"); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); 

希望答案将是有益的!

发生此错误是因为models/user.js尚未被router/index.js加载时解释。 解决这个问题的一个办法是做以下工作:

 var mongoose = require('mongoose'); //Load all your models var User = require('./../models/user.js'); //Now, this call won't fail because User has been added as a schema. mongoose.model('User'); 

然而,事实certificate这是违反最佳实践的,这就意味着所有这些configuration都会在你的app.js文件开始时发生。 看一下这个来自madhums示例项目的例子

 var models_path = __dirname + '/app/models' fs.readdirSync(models_path).forEach(function (file) { if (~file.indexOf('.js')) require(models_path + '/' + file) }) 

请注意,他正在加载他的模型之前设置应用程序的路由器。 至于Ubuntu vs Mac的问题,我相信这是因为Ubuntu的相对path必须以./开头。 您只需将其更改为可在Mac上运行的./../models/user.js

在加载模型文件加载之前尝试获取模型时发生此问题

我在我的mean.io项目中解决了同样的问题

在控制器中:

 'use strict'; require('../models/settingsModel'); // load settingsModel.js file before get mongoose.model('Settings') var mongoose = require('mongoose'), Settings = mongoose.model('Settings'), Q = require('q'); 

在其他文件中使用mongoose模式js文件中的所有代码应该已经运行。

例如,下面的代码片断确保mongoose模式文件/模块被执行。

fs.readdirSync(__ dirname +'/app/models').forEach(function(file){if(〜file.indexOf('。js'))require(__ dirname +'/ app / models /'+ file);} );

或模式文件可以通过调用手动执行

var User = require('./app/ models / user.js')

在模型在应用程序的任何地方使用之前。

完成上述操作后,可以要求/执行使用mongoose模型的其他模块。

我在尝试使用mongoose-fixture将一些默认数据input到mongo集合中时出现此错误。 被困惑了很长时间,以下和类似的线程寻求帮助,试图debugging。 最终问题原来是由于我的情况下mongoosemongoose-fixture版本。

如果没有代码更改帮助,或者如果您在尝试正确使用mongoose-fixture (应该为您注册模式)时出现此错误,请尝试此操作。 删除项目的node_modules目录,运行npm cache clean ,然后执行npm install

如果即使这样也没有帮助,请尝试比较有问题的应用程序和工作的应用程序之间的mongoose / mongoose-fixture版本,然后尝试更改package.json的版本,然后重复上述步骤。 这对我有效。

在使用快递时,常见的错误之一是要求在mongoose面前expression。 这导致“MissingSchemaError:架构尚未注册模型”用户“。” 错误。

你可以通过纠正“要求”命令(即mongoose然后expression)

 var mongoose = require('./config/mongoose'), express = require('./config/express'); 

当我尝试在MEAN堆栈上的本教程User Auth的基本代码上添加一个新模型时遇到了这个问题。 解决scheme就像Ahei提到的一样。

具体来说,我需要在app.js所需的/app_api/models/db.js结尾添加一行require('path/to/your/model') 。 为了便于开发,保持结构的一致性更好。

如果您正在使用Mean.js堆栈,请运行以下testing:

 grunt test 

而不是与

 mocha