Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Development Tools

Matt McLean
Matt McLean
2,733 Points

Failed connecting to MongoDB

Ive run mongod and the database seems to be running correctly but I am getting an error.

THanks Matt

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,956 Points

Hi Matt,

could you tell us what kind of error you're getting? If you're able to load databases in Mongo it might just be a typo in the command you're using. :-)

2 Answers

Matt McLean
Matt McLean
2,733 Points

Hi Jonathan- I'm doing this as part of the Building a MEAN Application course. I created my own error message to indicate if the application could connect to the database. Here is the database.js file:

'use strict';

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mean-todo', function(err) {
  if (err) {
    console.log('Failed connecting to MongoDB!');
  } else {
    console.log('Successfully connected to MongoDB!');
  }
});
Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,956 Points

i haven't taken the course yet but it sounds to me like you have the connection to mongoose setup correctly and the database connection set up, but you have the code set up to report that the connection has failed when in fact it hasn't.

Try switching the commands in your if block.

'use strict';

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mean-todo', function(err) {
  if (!err) {
    console.log('Failed connecting to MongoDB!');
  } else {
    console.log('Successfully connected to MongoDB!');
  }
});

Try switching the commands in your if blog or using the inequality operator. It may not be that simple but maybe worth a try :)