Marvine Chi
Marvine Chi

Reputation: 177

I am getting Error: failed to connect to [undefined:27017]

Not sure what to do, can't connect when i run >>node index.js in git shell. it gives me the following

LoL RPG started on port 8080 connection error: [Error: failed to connect to [undefined:27017]]

/* ==== MONGODB ==== */
var mongoose    = require('mongoose');
var db          = require('./config/db.js');

mongoose.connect(db.url);
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.once('open', function() { console.log("Mongo DB connected!"); });

 /* ==== config/db.js ==== */
module.exports = "mongodb://<username>:<username>@ds052837.mongolab.com:52837/lolrpg"; 

Upvotes: 3

Views: 9828

Answers (1)

influxd
influxd

Reputation: 536

The problem here is that your db variable in the first section of code is referring to the connection string, but you try to access a url property on it, which ends up being undefined.

Replace mongoose.connect(db.url) with mongoose.connect(db).

Alternatively, in db.js, you can replace module.exports = ... with module.exports.url = ....

Upvotes: 7

Related Questions