npm启动会带来错误



我正在学习本教程:https://thinkster.io/mean-stack-tutorial

我正处于开始使用mongodb 的阶段

要启动mongodb,我使用以下命令:sudo mongod &

我的app.js文件:

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;

和我的index.js文件(路由):

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;

然后,当我在终端中键入npm start时,我会收到以下错误:

jsantana@jsantana-Inspiron-3521:~/Documents/mean/flapper_news/flapper-news$ npm start
> flapper-news@0.0.0 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! flapper-news@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the flapper-news@0.0.0 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

下面是包含文件夹结构的github:https://github.com/jsantana90/meanstacktutorial

问题

如何修复此错误?我对MEAN堆栈和MongoDB还很陌生。

感谢您抽出时间!

在定义路线之前,您必须首先要求您的猫鼬模型

错误的是你有这个序列:

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 mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/news');
require('./models/Posts');
require('./models/Comments');
var routes = require('./routes/index');
var users = require('./routes/users');

相关内容

  • 没有找到相关文章

最新更新