简体   繁体   中英

MissingSchema Error: Schema hasn't been registered for model “User”

I keep getting this error whenever I launch my application:

--MissingSchema Error: Schema hasn't been registered for model "User" --

I'm working a tutorial from the "Mongoose for Application Development" book by Simon Holmes. I'm at Chapter 5 "Interacting with Data - Creation"

Here's my code:

app.js:

var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, project = require('./routes/project') 
, http = require('http')
, path = require('path');

db.js:

//Creating the Application Schemas:
//====================================


//User Schema:
//===============
var userSchema = new mongoose.Schema({
name: String,
email: {type: String, unique:true},
createdOn: { type: date, default: date.now },
modifiedOn: Date,
LastLogin: Date  
});

//Build the User Model:
//===========================
mongoose.model( 'User', userSchema );

User.js:

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

ERROR THAT RESULTS:

throw new mongoose.Error.MissingSchemaError(name);
  ^
MissingSchemaError: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema) at Mongoose.Model             (C:\Users\New\Desktop\mongoose_pm_app\
mongoosepm\node_modules\mongoose\lib\index.js.311.13)
at Object. <anonymous>    (C:\Users\New\Desktop\mongoose_pm_app\mongoosepm\routes\user.js:2:21)
atModule._compile (module.js:456:26)
atObject.Module._extensions..js (module.js:474:10)
atModule.load (module.js:356:32)
at Function.Module._load (module.js:364:17)
at require (module.js:380:17)
at Object <anonymous> (C:\Users\New\Desktop\mongoose_pm_app\mongoosepm\app.js:8:12)
at Module._compile (module.js:456:26) 
25 June 19:52:55 - [nodemon] app crashed waiting for file changes before starting...

I'm young to mongoose and mongodb. I've been through the books errata pages to check if I mistyped anything but its all the same as here.

I also tried downloading the sample code from PACKT, the sample code looks the same.

Any and all assistance would be Greatly appreciated. Thanks.

You need to require your db.js file someplace, as otherwise the code in it will never run, and the schema will never be registered.

Also, I would recommend that you define your user schema inside of User.js and add the model to exports, like so:

User.js

//User Schema:
//===============
var userSchema = new mongoose.Schema({
name: String,
email: {type: String, unique:true},
createdOn: { type: date, default: date.now },
modifiedOn: Date,
LastLogin: Date  
});

//Build the User Model:
//===========================
var User = mongoose.model( 'User', userSchema );

//Exports
//=======
exports = User;

This allows you to do just require the file elsewhere to get a reference to the User model (instead of having to retrieve it through mongoose.model ). For example:

var User = require('User');
// ...
var fred = new User({ ... });    // create a new user
fred.save(function (err, user) { ... });

I've not read the book that you referred to, so I'm not sure if there might be a good reason to define all your schemas in one file (which is the direction it looked like you were going in with db.js). From what I've seen, it's more common to have one schema/model per file.

That error comes from one line:

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

You need to provide a Schema object to mongoose.model() method. The first param, 'User', just tells mongoose in what will the collection be named. The second one defines the user schema.

So either do something like Sergey has answered, or add these few changes.

To your db.js, add export line at the bottom:

module.exports = userSchema;

Then require it in User.js:

var mongoose = require("mongoose");
var userSchema = require("./db.js");
var User = mongoose.model( 'User', userSchema );
module.exports = User;

Alternatively you can just alter the last line in your Schema definition, by exporting the user model when you build it:

//Build the User Model:
//===========================
module.exports = mongoose.model( 'User', userSchema );

Then you don't need User.js at all.

Although, then your file naming doesn't follow convention, if you care about it.

Advantages of separating Schema and Model are when you require multiple complex schemas, when you have a lot of schema plugins so you want a simpler model file where you require all that. If you only have a simple schema like above, I recommend that you use a single-file version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM