作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我一直在用头撞墙。我一直在做关于基本 Node.js 的 lynda.com 教程,我被困在这一点上。
当我输入 nodemon server.js 时出现错误。我在导出 node.js 中的数据库连接时遇到问题。有什么提示吗?有没有更好的调试方法,然后在 Node 中进行更改和刷新?
/Users/khebert/Sites/node/airline/node_modules/connect-mongo/lib/connect-mongo.js:87
this.db = new mongo.Db(options.mongoose_connection.db.databaseName,
^
TypeError: Cannot read property 'databaseName' of undefined
at new MongoStore (/Users/khebert/Sites/node/airline/node_modules/connect-mongo/lib/connect-mongo.js:87:60)
at module.exports (/Users/khebert/Sites/node/airline/app.js:22:10)
at Object.<anonymous> (/Users/khebert/Sites/node/airline/server.js:4:27)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
2 Jan 08:04:56 - [nodemon] app crashed - waiting for file changes before starting...
db.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/flights', function(err) {
if (err) console.log(err);
else console.log('DB success');
});
module.exports = mongoose.connections;
服务器.js
var http = require('http'),
flights = require('./data'),
db = require('./db'),
app = require('./app.js')(flights, db);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
应用程序.js
module.exports = function(flights, db) {
/**
* Module dependencies.
*/
var express = require('express');
var MongoStore = require('connect-mongo')(express);
var routes = require('./routes')(flights);
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.session({
secret: 'keyboard cat',
store: new MongoStore({
mongoose_connection: db
})
}));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(function (req, res, next) {
res.set('X-Powerd-By','Flight Tracker');
next();
});
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' === app.get('env')) {
app.use(express.errorHandler());
}
app.get('/flight/:number', routes.flight);
app.put('/flight/:number/arrived', routes.arrived);
app.get('/list', routes.list);
app.get('/arrivals', routes.arrivals);
return app;
};
最佳答案
这可能是“express.bodyParser()”的问题,您必须在应用的所有配置之后定义您的连接。
关于node.js - 类型错误 : Cannot read property 'databaseName' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20886937/
我是一名优秀的程序员,十分优秀!