Johhan Santana
Johhan Santana

Reputation: 2415

npm start brings errors

I'm following this tutorial: https://thinkster.io/mean-stack-tutorial

I'm at the part where I am beginning to work with mongodb

To start mongodb I use this command: sudo mongod &

My app.js file:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');
var mongoose = require('mongoose');
require('./models/Posts');
require('./models/Comments');

mongoose.connect('mongodb://localhost/news');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;

and my index.js file (routes):

var express = require('express');
var router = express.Router();

var mongoose = require('mongoose');
var Post = mongoose.model('Post');
var Comment = mongoose.model('Comment');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/posts', function(req, res, next){
    var post = new Post(req.body);

    Post.save(function(err, posts){
        if(err){return next(err);}

        res.json(posts);
    });
});


module.exports = router;

Then when I type npm start in my terminal I get these errors:

jsantana@jsantana-Inspiron-3521:~/Documents/mean/flapper_news/flapper-news$ npm start

> [email protected] start /home/jsantana/Documents/mean/flapper_news/flapper-news
> node ./bin/www


/home/jsantana/Documents/mean/flapper_news/flapper-news/node_modules/mongoose/lib/index.js:333
      throw new mongoose.Error.MissingSchemaError(name);
      ^
MissingSchemaError: Schema hasn't been registered for model "Post".
Use mongoose.model(name, schema)
    at Mongoose.model (/home/jsantana/Documents/mean/flapper_news/flapper-news/node_modules/mongoose/lib/index.js:333:13)
    at Object.<anonymous> (/home/jsantana/Documents/mean/flapper_news/flapper-news/routes/index.js:5:21)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/home/jsantana/Documents/mean/flapper_news/flapper-news/app.js:8:14)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/home/jsantana/Documents/mean/flapper_news/flapper-news/bin/www:7:11)

npm ERR! Linux 3.16.0-51-generic
npm ERR! argv "/home/jsantana/local/bin/node" "/home/jsantana/local/bin/npm" "start"
npm ERR! node v5.0.0
npm ERR! npm  v3.3.6
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script 'node ./bin/www'.
npm ERR! This is most likely a problem with the flapper-news package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get their info via:
npm ERR!     npm owner ls flapper-news
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/jsantana/Documents/mean/flapper_news/flapper-news/npm-debug.log

Here's the github containing the folder structure: https://github.com/jsantana90/meanstacktutorial

question

How do I fix this error? I'm quite new to the MEAN stack and MongoDB.

Thank you for your time!

Upvotes: 1

Views: 210

Answers (1)

Scott Stensland
Scott Stensland

Reputation: 28285

You must first require your mongoose models prior to defining routes

Incorrectly you have this sequence :

var routes = require('./routes/index');
var users = require('./routes/users');
var mongoose = require('mongoose');
require('./models/Posts');
require('./models/Comments');

mongoose.connect('mongodb://localhost/news');

try this sequence :

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/news');

require('./models/Posts');
require('./models/Comments');

var routes = require('./routes/index');
var users = require('./routes/users');

Upvotes: 3

Related Questions