You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

搭建用户认证系统时遭遇MissingSchemaError问题求助

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

Hey there, let's break down why you're running into this error and fix it step by step. I've gone through your code, and there are a couple of critical issues that are causing the schema registration problem.

1. Your User Schema isn't being registered in Mongoose

Looking at your Users.js file, you've defined the schema but never actually registered it as a model with Mongoose. The commented-out code at the bottom was close, but you need to properly register it so Mongoose can recognize the "User" model elsewhere in your app.

Fix for Users.js

Update the file to register the model (and optionally export it, though registering it directly with mongoose.model is enough for Mongoose to track it):

var mongoose = require ('mongoose');
var crypto = require ('crypto');
var jwt = require('jsonwebtoken');

var userSchema = new mongoose.Schema({
 email:{ type: String, unique: true, required: true },
 name: { type: String, required: true },
 hash: String,
 salt: String
});

// ... (keep all your schema methods here: setPassword, validPassword, generateJwt)

// Register the schema as a model with Mongoose
mongoose.model('User', userSchema);

// Optional: Export the model if you need to require it directly elsewhere
// module.exports = mongoose.model('User', userSchema);

2. Incorrect path when requiring the User model in app.js

In your app.js, you have require('./users'); but it's likely your Users.js file is located in the app_api/models directory (based on other imports like require('./app_api/models/db');). If the path is wrong, Node.js won't load the file, so Mongoose never gets the schema registration.

Fix for app.js

Update the require path to match where your Users.js is actually located. For example, if it's in app_api/models:

// Replace this line:
// require('./users');
// With this:
require('./app_api/models/Users');

Note: Make sure the filename matches exactly (case-sensitive on most systems) — if your file is named Users.js, use Users, not users.

3. Bug in authentication.js that will cause another error

I noticed the last line of authentication.js is module.exports = router;, but you never defined router anywhere in that file. This will throw an error when you try to use this module. If you're using this file as part of your API routes, you need to create an Express Router first:

Fix for authentication.js

Add the router setup at the top of the file, and make sure your register/login methods are attached to it:

var passport = require('passport');
var mongoose = require('mongoose');
var User = mongoose.model('User');
// Add this line to create the router
var express = require('express');
var router = express.Router();

var sendJSONreponse = function(res, status, content) {
 res.status(status);
 res.json(content);
};

// Register controller (attach to router)
router.post('/register', function(req, res){
 // ... keep your existing register logic here
});

// Login controller (attach to router)
router.post('/login', function(req, res) {
 // ... keep your existing login logic here
});

// Now export the router correctly
module.exports = router;

Final Checks

  • Make sure your app.js is connecting to MongoDB before requiring the User model (you already have this right, since mongoose.connect is above the require('./users') line — good job!)
  • Double-check that process.env.JWT_SECRET is set correctly in your .env file, otherwise the generateJwt method will throw an error.

Once you make these changes, the MissingSchemaError should be gone, and your auth system should start working as expected.

内容的提问来源于stack exchange,提问作者zp24

火山引擎 最新活动