Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMatt McLean
2,733 PointsFailed connecting to MongoDB
Ive run mongod and the database seems to be running correctly but I am getting an error.
THanks Matt
2 Answers
Matt McLean
2,733 PointsHi 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
Treehouse Moderator 91,253 Pointsi 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 :)
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsHi 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. :-)