2016-12-22 11 views
0

MEANスタックプロジェクトを作成するのに、現在this tutorialに従っています。プロジェクトのこの段階では、Expressを使用してMongoデータベースの休憩ルートを開きます。ここでは、/routes/index.js(express -ejsで自動作成)の先頭に追加するコードを示します。Thinkster MEANスタックチュートリアル、ExpressJSでREST APIを設定/設定するときのエラー

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

私はNPM開始を使用してサーバを実行しようとすると、NPMは./bin/www初期スクリプトに失敗し、終了ステータス1で終了します。デバッグログと、/routes/index.jsのコードと、Postスキーマが作成されたファイル/models/Posts.jsがあります。誰もこれを解決する方法を知っていますか?それ以上の情報が必要なら私に知らせてください。あなたは "をエクスポートする必要が

./routes/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' }); 
}); 

module.exports = router; 

./models/Posts.js

var mongoose = require('mongoose'); 
var PostSchema = new mongoose.Schema({ 
    title: String, 
    link: String, 
    upvotes: {type: Number, default: 0}, 
    comments: [{type: mongoose.Schema.Types.ObjectId, ref: 'Comment'}] 
}); 

mongoose.model('Post', PostSchema); 

NPM-debug.logに

0 info it worked if it ends with ok 
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'start' ] 
2 info using [email protected] 
3 info using [email protected] 
4 verbose run-script [ 'prestart', 'start', 'poststart' ] 
5 info lifecycle [email protected]~prestart: [email protected] 
6 silly lifecycle [email protected]~prestart: no script for prestart,continuing 
7 info lifecycle [email protected]~start: [email protected] 
8 verbose lifecycle [email protected]~start: unsafe-perm in lifecycle true 
9 verbose lifecycle [email protected]~start: PATH: /usr/lib/node_modules/npm/bin/node-gyp-bin:/home/user/Dropbox/Coding/redditClone/bluenews/node_modules/.bin:/usr/local/heroku/bin:/home/user/anaconda2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin 
10 verbose lifecycle [email protected]~start: CWD: /home/user/Dropbox/Coding/redditClone/bluenews 
11 silly lifecycle [email protected]~start: Args: [ '-c', 'node ./bin/www' ] 
12 silly lifecycle [email protected]~start: Returned: code: 1 signal: null 
13 info lifecycle [email protected]~start: Failed to exec start script 
14 verbose stack Error: [email protected] start: `node ./bin/www` 
14 verbose stack Exit status 1 
14 verbose stack  at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/lib/utils/lifecycle.js:255:16) 
14 verbose stack  at emitTwo (events.js:106:13) 
14 verbose stack  at EventEmitter.emit (events.js:191:7) 
14 verbose stack  at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/lib/utils/spawn.js:40:14) 
14 verbose stack  at emitTwo (events.js:106:13) 
14 verbose stack  at ChildProcess.emit (events.js:191:7) 
14 verbose stack  at maybeClose (internal/child_process.js:885:16) 
14 verbose stack  at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5) 
15 verbose pkgid [email protected] 
16 verbose cwd /home/user/Dropbox/Coding/redditClone/bluenews 
17 error Linux 4.4.0-53-generic 
18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "start" 
19 error node v7.2.1 
20 error npm v3.10.10 
21 error code ELIFECYCLE 
22 error [email protected] start: `node ./bin/www` 
22 error Exit status 1 
23 error Failed at the [email protected] start script 'node ./bin/www'. 
23 error Make sure you have the latest version of node.js and npm installed. 
23 error If you do, this is most likely a problem with the bluenews package, 
23 error not with npm itself. 
23 error Tell the author that this fails on your system: 
23 error  node ./bin/www 
23 error You can get information on how to open an issue for this project with: 
23 error  npm bugs bluenews 
23 error Or if that isn't available, you can get their info via: 
23 error  npm owner ls bluenews 
23 error There is likely additional logging output above. 
24 verbose exit [ 1, true ] 

答えて

0

あなたのPost.jsファイルに「Post」と書かれていれば、他のモジュールから見えます。あなたのケースでは

、あなたは以下を行う必要があります。また、index.jsにパスモジュールを含める必要があり

var Post = mongoose.model('./models/Posts.js'); 

以下のよう

var Post=mongoose.model('Post', PostSchema); 
module.exports=Post; 

ステップして、あなたはこのファイルをインポートする必要があります速達=は( '表現')必要ラインVAR後

var path = require('path'); 

;: 元ファイル var router = express.Router();

+0

ありがとうございます!これは私が問題を捜索するのを助けました。それは、マングーススキーマが認識されていないことでした。 app.jsのインポートファイルをモデルファイルの前にルートファイルを必要としていました。他の誰かが問題を抱えている場合は、[こちらがstackoverflow](http://stackoverflow.com/questions/26762913/angularjs-schema-hasnt-been-registered-for-model-post)トラブルシューティングの後に見つけました。 – ReforMatt

関連する問題