简体   繁体   中英

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

I can't resolve an issue with mongoose throwing error

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

I included all paths, checked it according to my another Schemas and everything seems alright. Am I missing something?

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;
};

APPOINTMENT.SERVER.MODEL.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});

APPOINTMENTS.SERVER.CONTROLLER.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);
        }

    });
};

APPOINTMENT.SERVER.ROUTES.JS:

var appointments = require('../../app/controllers/appointment.server.controller');

module.exports = function(app) {
    app.route('/appointment').post(appointments.create);
};

Error seems to say that I didn't create Appointment Schema, which I did in APPOINTMENT.SERVER.MODEL.JS:

没关系,好​​像我搞砸了猫鼬配置本身...问题解决了!

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