mongoose插件抛出错误:MissingSchemaError:Schema没有't已注册为型号“t”;预约

mongoose plugin throws an error: MissingSchemaError: Schema hasn't been registered for model "Appointment"

本文关键字:注册 预约 出错 插件 错误 MissingSchemaError 没有 Schema mongoose      更新时间:2023-09-26

我无法解决猫鼬投掷错误的问题

throw new mongoose.Error.MissingSchemaError(name);
      ^
MissingSchemaError: Schema hasn't been registered for model "Appointment".
Use mongoose.model(name, schema)

我包括了所有的路径,根据我的另一个Schemas进行了检查,一切似乎都很好。我是不是错过了什么?

EXPRESS.JS:

var config = require('./config'),
express = require('express'),
morgan = require('morgan'),
compress = require('compression'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
session = require('express-session');
module.exports = function() {
    var app = express();

if (process.env.NODE_ENV === 'development') {
    app.use(morgan('dev'));
} else if(process.env.NODE_ENV === 'production') {
    app.use(compress());
}
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(session({
    saveUninitialized: true,
    resave: true,
    secret: 'config.sessionSecret'
}));
app.set('views', './app/views');
app.set('view engine', 'ejs');
require('../app/routes/index.server.routes')(app);
require('../app/routes/users.server.routes')(app);
require('../app/routes/appointment.server.routes')(app);
app.use(express.static('./public'));
return app;
};

任命.服务器.模型.JS:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;
var AppointmentSchema = new Schema({
    UserID: {
        type: Schema.ObjectId,
        ref: User,
        private: true
    },
    time: {
        type: String,
        get: function(){
            for (var i in Schedule.freeTime){
                if (i === this.time) {
                    return this.time
                }
            }
        }
    }
});
mongoose.model('Appointment', AppointmentSchema);
AppointmentSchema.set('toJSON', {getters: true});

任命.服务器.控制器.JS:

var Appointment = require('mongoose').model('Appointment'),
    User = require('./user.server.controller');
exports.create = function(req,res,next) {
    var appointment = new Appointment(req.body);
    appointment.UserID = User;
    appointment.save(function (err) {
        if (err){
            return next(err);
        } else{
            res.json(appointment);
        }
    });
};

预约.服务器.路由.JS:

var appointments = require('../../app/controllers/appointment.server.controller');
module.exports = function(app) {
    app.route('/appointment').post(appointments.create);
};

错误似乎表明我没有创建Appointment架构,这是我在APPOINTMENT.SERVER.MODEL.JS:

中创建的

Nevermind,看起来我把猫鼬配置本身搞砸了。。。问题解决了!