MissingSchemaError:尚未为模型“用户"注册架构 [英] MissingSchemaError: Schema hasn't been registered for model "User"
问题描述
在我的 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
中,我会(在最后一行)
If however, in user.js
, I do (in the last line)
module.exports = mongoose.model('User', userSchema);
在 index.js
中,我执行 var User = require('../models/User');
,然后一切正常.
and in index.js
I do var User = require('../models/User');
, then everything works.
但它不应该,因为在 config/pass.js
我正在做 var User = mongoose.model('User');
并且它完美地工作.
But it should not, because in config/pass.js
I am doing var User = mongoose.model('User');
and it's working flawlessly.
require('../models/User');
语法不适用于 Ubuntu,但适用于我的 Mac.
The require('../models/User');
syntax isn't working on Ubuntu, but is on my Mac.
我该怎么办?我如何解决它?我查看了大量示例应用程序,包括 MEAN 但没有什么是真正有用的.
What should I do? How do I fix it? I have looked at tons of sample apps, including MEAN but nothing was really helpful.
推荐答案
我在尝试 MEAN 教程时遇到了同样的问题.
I got the same problem when I am trying the MEAN tutorial.
经过一番研究,我发现在 app.js 中,如果我将 require("./models/User") 放在 var routes = require("./routes/index") 之前,那么它就可以工作.
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.
像这样:
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屋!