MissingSchemaError:模式“用户”尚未注册模式。 [英] MissingSchemaError: Schema hasn't been registered for model "User"

查看:114
本文介绍了MissingSchemaError:模式“用户”尚未注册模式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var mongoose = require('mongoose'); 
var Schema = mongoose.Schema;

var userSchema = new Schema({
(define schema)
});
...
(保存用户)
...
(检查密码)
...
mongoose.model('User',userSchema );

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

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

会抛出错误:

  MissingSchemaError:模式User尚未注册模式。 

然而,如果在 user.js 我做(最后一行)

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

index.js code> var User = require('../ models / User'); ,然后一切正常。



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



require('../ models / User'); 语法不是在Ubuntu上工作,但是在我的Mac上。



我该怎么办?如何解决?我已经查看了大量的示例应用程序,包括 MEAN ,但没有什么是真正有用的

解决方案

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



在完成了一些研究之后,我发现在app.js中,如果我将require(./ models / User)放在var routes = require( ./routes/index),那么它有效。



像这样:






  mongoose.connect( mongodb的://本地主机/新闻); 
require(./ models / Posts);
require(./ models / Comments);

var routes = require('./ routes / index');
var users = require('./ routes / users');

var app = express();






希望答案将会有帮助! >

In my models/user.js file:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

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

And in my router/index.js, I have:

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

which throws the error:

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

If however, in user.js, I do (in the last line)

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

and in index.js I do var User = require('../models/User');, then everything works.

But it should not, because in config/pass.js I am doing var User = mongoose.model('User'); and it's working flawlessly.

The require('../models/User'); syntax isn't working on Ubuntu, but is on my Mac.

What should I do? How do I fix it? I have looked at tons of sample apps, including MEAN but nothing was really helpful.

解决方案

I got the same problem when I am trying the MEAN tutorial.

After done a little bit research, I found that in app.js, if I put require("./models/User") before var routes = require("./routes/index"), then it works.

Like this:


mongoose.connect("mongodb://localhost/news");
require("./models/Posts");
require("./models/Comments");

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();


Hope the answer will be helpful!

这篇关于MissingSchemaError:模式“用户”尚未注册模式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆