简体   繁体   中英

Can't connect Mongodb to Node.js : throw er; // Unhandled 'error' event ^ Error: failed to connect to [undefined:27017]

I'm trying to connect a Mongoose with my Node.js but it always show this error. I am following this tutorial to create a authentication system. After writing the server.js file and trying to fix a problem in the routes.js I got an error and I don't know why, please help.

Here are my code and error :

server.js

 // server.js // set up ====================================================================== // 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.url); // 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: 'ilovescotchscotchyscotchscotch' })); // 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); 

routes.js :

 module.exports = function(app, passport) { //... }; 

package.json

 { "name": "node-authentication", "main": "server.js", "dependencies" : { "express" : "~4.0.0", "ejs" : "~0.8.5", "mongoose" : "~3.8.1", "passport" : "~0.1.17", "passport-local" : "~0.1.6", "passport-facebook" : "~1.0.2", "passport-twitter" : "~1.0.2", "passport-google-oauth" : "~0.1.5", "connect-flash" : "~0.1.1", "bcrypt-nodejs" : "latest", "morgan": "~1.0.0", "body-parser": "~1.0.0", "cookie-parser": "~1.0.0", "method-override": "~1.0.0", "express-session": "~1.0.0" } } 
database.js

 // config/database.js module.exports = { 'url' : 'your-settings-here' // looks like mongodb://<user>:<pass>@mongo.onmodulus.net:27017/Mikha4ot }; 

error

 The magic happens on port 8080 events.js:141 throw er; // Unhandled 'error' event ^ Error: failed to connect to [undefined:27017] at null.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ouestcharlie/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:556:25) at emitThree (events.js:97:13) at emit (events.js:175:7) at null.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ouestcharlie/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:156:15) at emitTwo (events.js:87:13) at emit (events.js:172:7) at Socket.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ouestcharlie/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10) at emitOne (events.js:77:13) at Socket.emit (events.js:169:7) at connectErrorNT (net.js:996:8) at nextTickCallbackWith2Args (node.js:442:9) at process._tickCallback (node.js:356:17) 

Mongoose.connect() expects parameters in the format:

"mongodb://<hostname>/<databasename>"

instead, it's getting what's configured in your database.js, which is the following:

"your-settings-here"

I'm guessing that that's going wrong. Just fill out a decent connection url in database.js, such as: "mongodb://localhost/my-test-db" (please replace with the proper values), and this error will go away.

This is the minimum needed to connect the myapp database running locally on the default port (27017). If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed. [source : http://mongoosejs.com/docs/connections.html]

I had the same error. I reinstalled mongoDB but it didn't work.

I solved my issue by using the following url to connect to localhost. mongoose.connect('mongodb://127.0.0.1:27017/database-name') ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM