TypeError:无法读取未定义的属性“databaseName”

时间:2014-01-02 16:13:08

标签: node.js mongodb mongoose connect

我一直在撞墙。我一直在关于必不可少的Node.js上做lynda.com tutorail而且我已经陷入了困境。

当我键入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;

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

app.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;
};

3 个答案:

答案 0 :(得分:1)

快递可能是一个问题。在快递应用程序的所有配置之后,你必须定义你的连接。

答案 1 :(得分:0)

在db.js中,

module.exports = mongoose.connections;

更改为:

module.exports = mongoose.connections[0];

答案 2 :(得分:0)

您可以重用已经由mongoose实例化的db对象!

new MongoStore({
  db: mongoose.connection.db
})