I'm using the Wes Boss Node.js tutorial and have been encountering a number of issues with schema errors.
My database is currently running on mLab and MongoDB Compass. It was fine yesterday when I left for work, and I had just added my first bit of data to the DB successfully. This morning I go to continue where I left off, and everything is suddenly broken.
I've tried deleting the node_modules
directory, running npm cache clean
, and npm install
. I have tried changing the order of the dependencies. I thought it might be that the connection just needed restarted, so I closed the connection, exited Compass, re-opened and re-connected to the DB. I tried deleting the "sessions" table and re-connecting. No such luck.
I've tried plugging the database's server address into my browser's URL bar and I receive a message indicating that the connection was successful.
Error:
MissingSchemaError: Schema hasn't been registered for model "Store". Use mongoose.model(name, schema) at MissingSchemaError (C:\Users\Misha\Desktop\dang-thats-delicious\node_modules\mongoose\lib\error\missingSchema.js:20:11)
app.js:
const express = require('express');const session = require('express-session');const mongoose = require('mongoose');const MongoStore = require('connect-mongo')(session);const path = require('path');const cookieParser = require('cookie-parser');const bodyParser = require('body-parser');const passport = require('passport');const promisify = require('es6-promisify');const flash = require('connect-flash');const expressValidator = require('express-validator');const routes = require('./routes/index');const helpers = require('./helpers');const errorHandlers = require('./handlers/errorHandlers');const app = express();app.set('views', path.join(__dirname, 'views'));app.set('view engine', 'pug');app.use(express.static(path.join(__dirname, 'public')));app.use(bodyParser.json());app.use(bodyParser.urlencoded({ extended: true }));app.use(expressValidator());app.use(cookieParser());app.use(session({ secret: process.env.SECRET, key: process.env.KEY, resave: false, saveUninitialized: false, store: new MongoStore({ mongooseConnection: mongoose.connection })}));app.use(passport.initialize());app.use(passport.session());app.use(flash());app.use((req, res, next) => { res.locals.h = helpers; res.locals.flashes = req.flash(); res.locals.user = req.user || null; res.locals.currentPath = req.path; next();});app.use((req, res, next) => { req.login = promisify(req.login, req); next();});app.use('/', routes);app.use(errorHandlers.notFound);app.use(errorHandlers.flashValidationErrors);if (app.get('env') === 'development') { app.use(errorHandlers.developmentErrors);}app.use(errorHandlers.productionErrors);module.exports = app;
index.js:
const express = require('express');const router = express.Router();const storeController = require('../controllers/storeController');const { catchErrors } = require('../handlers/errorHandlers');router.get('/', storeController.homePage);router.get('/add', storeController.addStore);router.post('/add', catchErrors(storeController.createStore));module.exports = router;
start.js:
require('./models/Store');const mongoose = require('mongoose');const Store = mongoose.model('Store');require('dotenv').config({ path: 'variables.env' });mongoose.connect(process.env.DATABASE);mongoose.Promise = global.Promise;mongoose.connection.on('error', (err) => { console.error(`${err.message}`);});require('./models/Store');const app = require('./app');app.set('port', process.env.PORT || 7777);const server = app.listen(app.get('port'), () => { console.log(`Express running → PORT ${server.address().port}`);});