无法通过js连接到mongo db

时间:2015-05-09 17:59:52

标签: javascript node.js mongodb

我编写了一个代码,用于将所有类型的社交网络日志与nodejs集成在一起。这是我的 server.js

// get all the tools we need
var express  = require('express');
var app      = express();
var port     = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var flash    = require('connect-flash');

var morgan       = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser   = require('body-parser');
var session      = require('express-session');

var configDB = require('./config/database.js');

// configuration ===============================================================
mongoose.connect(configDB); // connect to our database

// require('./config/passport')(passport); // pass passport for configuration

// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser()); // get information from html forms

app.set('view engine', 'ejs'); // set up ejs for templating

// required for passport
app.use(session({ secret: '234545671290eftg5678qwer235623' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session

// routes ======================================================================
//require('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport

// launch ======================================================================
app.listen(port);
console.log('The magic happens on port ' + port);    

配置/ database.js

module.exports = {

    'url' : 'mongodb://localhost27017:' // looks like mongodb://<user>:<pass>@mongo.onmodulus.net:27017/Mikha4ot

};

当我运行node server时,我收到以下错误:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: failed to connect to [[object Object]:27017]
    at null.<anonymous> (/home/ajay/Desktop/NodeAuthentication/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:555:74)
    at emit (events.js:106:17)
    at null.<anonymous> (/home/ajay/Desktop/NodeAuthentication/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:156:15)
    at emit (events.js:98:17)
    at Socket.<anonymous> (/home/ajay/Desktop/NodeAuthentication/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
    at Socket.emit (events.js:95:17)
    at net.js:834:16
    at process._tickCallback (node.js:448:13)  

我在这两行之间感到困惑:

var configDB = require('./config/database.js');  

mongoose.connect(configDB); // connect to our database  

我应该使用

mongoose.connect(configDB.url); ??

我该如何解决?

1 个答案:

答案 0 :(得分:1)

config / database.js 中存在拼写错误。您没有指定数据库名称,冒号必须介于localhost27017之间:

module.exports = {
    'url' : 'mongodb://localhost:27017/my-test-db' // looks like mongodb://<user>:<pass>@mongo.onmodulus.net:27017/Mikha4ot
};

在connect方法中,您可以放置​​Object或String,因此您应该按照提议使用它:

mongoose.connect(configDB.url);