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.

JavaScript Building a MEAN Application Setting Up MongoDB Using MongoDB with Express and Mongoose

Akshit Jindal
Akshit Jindal
1,341 Points

Failed Mongodb Connection?

I am doing the same thing as done in the vedio but i am getting failed mongodb connection.

database.js

'use strict';

var mongoose=require('mongoose');

mongoose.connect('mongodb://localhost/mean-todo',{useMongoClient:true},function(err){ if(err) { console.log('Failed mongodb conection!'); } else { console.log('Successful mongodb conection!'); } });

Have you tried to do:

mongoose.connect('mongodb://localhost:27017/mean-todo')
const db = mongoose.connection;
db.on("connection" ()=>{
        console.log('connected');
});
db.on("error", ()=>{
         console.log('error');
});

Or use your approach with the callback, but you may need to provide the port number which is 27017 (default)

Akshit Jindal
Akshit Jindal
1,341 Points

Even after using this solution, error is coming.

Are you starting the mongodb server with mongod in a seperate terminal window/tab?