【问题标题】:Thinkster MEAN Stack tutorial, error when routing/setting up REST apis with expressJSThinkster MEAN Stack 教程,使用 expressJS 路由/设置 REST api 时出错
【发布时间】:2016-12-22 20:37:25
【问题描述】:

我目前正在关注 this tutorial 创建一个 MEAN 堆栈项目。在项目的这个阶段,我正在使用 Express 在 Mongo 数据库上开辟休息路线。这是我添加到 /routes/index.js 顶部的代码(使用 express --ejs 自动创建)。

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

当我尝试使用 npm start 运行服务器时,npm 以退出状态 1 退出,在 ./bin/www 初始脚本上失败。这是调试日志以及 /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 npm@3.10.10
3 info using node@v7.2.1
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle bluenews@0.0.0~prestart: bluenews@0.0.0
6 silly lifecycle bluenews@0.0.0~prestart: no script for prestart,continuing
7 info lifecycle bluenews@0.0.0~start: bluenews@0.0.0
8 verbose lifecycle bluenews@0.0.0~start: unsafe-perm in lifecycle true
9 verbose lifecycle bluenews@0.0.0~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 bluenews@0.0.0~start: CWD: /home/user/Dropbox/Coding/redditClone/bluenews
11 silly lifecycle bluenews@0.0.0~start: Args: [ '-c', 'node ./bin/www' ]
12 silly lifecycle bluenews@0.0.0~start: Returned: code: 1  signal: null
13 info lifecycle bluenews@0.0.0~start: Failed to exec start script
14 verbose stack Error: bluenews@0.0.0 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 bluenews@0.0.0
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 bluenews@0.0.0 start: `node ./bin/www`
22 error Exit status 1
23 error Failed at the bluenews@0.0.0 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 ]

【问题讨论】:

    标签: javascript node.js mongodb express mean-stack


    【解决方案1】:

    您需要在 Post.js 文件中导出“Post”,然后只有它对其他模块可见。

    在您的情况下,您必须执行以下步骤

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

    然后你需要如下导入这个文件

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

    您还需要在 index.js 文件中包含路径模块 例如:

     var path = require('path');
    

    在 var express = require('express'); 之后 var router = express.Router();

    【讨论】:

    • 谢谢!这帮助我找到了问题,即猫鼬模式没有被识别。我把它归结为 app.js 中的导入在模型文件之前需要路由文件。对于其他有问题的人,here's the stackoverflow 经过更多故障排除后我找到了。
    猜你喜欢
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    相关资源
    最近更新 更多