npm start 带来错误
npm start brings errors
我正在学习本教程: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');
我正在学习本教程: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');