Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

javascript - MEAN Web Development

Currently I'm following along with the title named book. My code is as follows ( from the book so far as I can see it matches perfectly ). I apologize if this is something I could have easily searched for but I don't honestly understand the other answers well enough to figure it out on my end. Checking and rechecking the code from the book and what I have I don't see any errors I've made.

The MEAN Stack I'm using ( I wasn't aware there was more than one ) is MongoDB Express AngularJS and Nodejs

Error Msg ( Sorry I can't believe I initially forgot this )

e:MEAN
ode_modulesmongooselibindex.js:323
    throw new mongoose.Error.MissingSchemaError(name);
        ^
MongooseError: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema)
    at Mongoose.model (e:MEAN
ode_modulesmongooselibindex.js:323:13)
    at Object.<anonymous> (e:MEANappcontrollersusers.server.controller.js:1:94)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (e:MEANapp
outesusers.server.routes.js:1:75)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)

If it makes any difference this is the directory structure

app/
    controllers/
        users.server.controllers.js
    models/
        user.server.model.js
    routes/
        users.server.routes.js
config/
    express.js
    mongoose.js

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: false,
        secret: config.sessionSecret
    }));

    app.set('views', './app/views');
    app.set('view engine', 'ejs');

    require('../app/routes/index.server.routes.js')(app);
    require('../app/routes/users.server.routes.js')(app);

    app.use(express.static('./public'));

    return app;
};

mongoose.js

var config = require('./config'),
mongoose = require('mongoose');

module.exports = function() {
    var db = mongoose.connect(config.db);
    require('../app/models/user.server.model');
    return db;
}

user.server.model.js

var mongoose = require('mongoose'),
Schema = mongoose.Schema,
UserSchema;

UserSchema = new Schema({
    firstName: String,
    lastName: String,
    email: String,
    username: String,
    password: String
});

mongoose.model('User', UserSchema);

users.server.controllers.js

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

exports.create = function(req, res, next) {
    var user = new User(req.body);
    user.save(function(err) {
        if (err) {
            return next(err);
        } else {
            res.json(user);
        }
    })
};

users.server.routes.js

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

module.exports = function (app) {
    app.route('/users').post(users.create);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Missing a comma in server.js. Not sure what made me check it, but that was my issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...