mongoose插件抛出错误:MissingSchemaError:Schema尚未注册模型"约会"

时间:2015-12-28 14:56:23

标签: javascript node.js mongodb mongoose mongoose-schema

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

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

我包含了所有路径,根据我的另一个模式进行检查,一切似乎都没问题。我错过了什么吗?

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

错误似乎说我没有创建Appointment Schema,我在APPOINTMENT.SERVER.MODEL.JS中做了:

1 个答案:

答案 0 :(得分:0)

没关系,好​​像我搞乱了mongoose配置......问题解决了!