类型错误: 无法读取未定义的属性"数据库名称"



我一直在用头撞墙。我一直在做关于基本 Node 的 lynda.com 教程.js但我被困在这一点上。

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

分贝.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()'的问题,你必须在对应用程序进行所有配置后定义连接。

在 db.js 中,

module.exports = mongoose.connections;

更改为:

module.exports = mongoose.connections[0];

你可以重用猫鼬已经实例化的 db 对象!

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

相关内容

  • 没有找到相关文章

最新更新