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

express, javascript, mongoose, node.js

Solution

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!

Problem

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.

Original source